How can I check if a program is running for the first time?

后端 未结 3 1340
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 02:53

My program sets its display based on if the program is running for the first time or not. In order to determine if the program is running for the first time I am currently u

3条回答
  •  旧时难觅i
    2020-12-11 03:10

    Since your question appears to be concerned about each user that launches the application, then you should design a per-user solution.

    Using Properties.Settings will actually work and be efficient as long as the setting in question is user-specific.

    However, if this is not desired or appropriate for your application, you could also write a user-specific entry to the registry.

    For example:

            const string REGISTRY_KEY = @"HKEY_CURRENT_USER\MyApplication";
            const string REGISTY_VALUE = "FirstRun";
            if (Convert.ToInt32(Microsoft.Win32.Registry.GetValue(REGISTRY_KEY, REGISTY_VALUE, 0)) == 0)
            {
                lblGreetings.Text = "Welcome New User";
                //Change the value since the program has run once now
                Microsoft.Win32.Registry.SetValue(REGISTRY_KEY, REGISTY_VALUE, 1, Microsoft.Win32.RegistryValueKind.DWord);
            }
            else
            {
                lblGreetings.Text = "Welcome Back User";
            }
    

提交回复
热议问题