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:
Where in the registry are you storing the data between executions? Is everything in HKCU?
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?
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.
精彩评论