开发者

Registry Keys not persisting after machine restarts

开发者 https://www.devze.com 2023-03-02 06:08 出处:网络
I created an application that needs to remember some values (the state (checked/unchecked) of some checkboxes). The problem is the following:

I created an application that needs to remember some values (the state (checked/unchecked) of some checkboxes). The problem is the following:

If I c开发者_如何学Goheck/uncheck the checkboxes and restart application everything is fine. If I check/uncheck the checkboxes and restart the computer, on startup (I have run with windows activated) the application doesn't remember the actual values, but the values before that.

Now here's the odd part. If I close the application on the second scenario and open it again the values are set correct (even though I don't change them).

Also: If I check/uncheck the checkboxes and restart but don't have the start with windows checkbox active, if I open the application after windows start everything is fine.

The only time is wrong is when windows starts it by itself.

Here is the start with windows part of the program:

private void RunStartup(Boolean RunOnStartup)
    {
        Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        if (RunOnStartup == true)
        {
            key.SetValue("app", Application.ExecutablePath.ToString());
        }
        else
        {
            key.DeleteValue("app", false);
        }
    }


Is it possible that the program runs under a different after the computer starts up (i.e. system-account, since at that time no user is logged in)?

In that case, the Registry.CurrentUser... will be different from when you start the program interactively.


A few things to clarify your question:

  1. Where in the registry are you storing the data between executions? Is everything in HKCU?

  2. When you said it remembers "the values before that" do you mean from the last startup? Or, if you repeat the "change/restart app/change/restart app" process more than once before rebooting, does it always remember the second-to-last set of values?

  3. Are you restarting the computer with your application already open? Is it possible you're not saving state properly on a Windows shutdown event?

One thing to try here is to add some logging, e.g. log4net, to the part of your code that does the restore and verify that you are actually reading from the same place every time.

0

精彩评论

暂无评论...
验证码 换一张
取 消