this bug is pretty unusual. Basically my code will change the Settings.Default.Example
then save and restart the program. Then when it loads, it shows a message box. However oddly, it shows a empty value when the form loads.
Here is my code:
Main.cs
private void Button1_Click(object sender, EventArgs e)
{
Settings.Default.Example = "Somevalue"; //Sets a value to the settings
Settings.Default.Save(); // Save it
MessageBox.Show(Settings.Default.Example); //Confirming it has been saved
Application.Restart();
}
private void Main_Load(object sender, EventArgs e)
{
MessageBox.Show(Settings.Default.Example); // Here is the weird part, it shows empty.
}
The MessageBox
will show "Somevalue" when the button was clicked then the applcation restarts and the MessageBox
that showed was empty. However repeating the process by clicking the button once more and restarting it does show the "Somevalue" MessageBox
. Please help! Many Thanks!
If your
AssemblyInfo.cs
file has a*
in Assembly Version then it's refreshing the file every build so you won't see persistence or reliability until you change that to a hard number and rebuild all, then retest everything.did you try calling
ConfigurationManager.RefreshSection
before checking that it got saved ad you could try it again after the reloadAfter a full day of researching and studying the subject, I was able to solve this by putting the Configuration to the user:
Maybe you ran the same mistake as I did: setting the setting's
scope
toApplication
. Those kind of settings are not saved.Set it to
User
to solve the problem.rene is correct - you need to call
Default.Reload
after calling theSave
method:Possibly a bug - ?
Posting as a reply to increase visibility -
If you need to test your application how actually working in this case better to run the exe file. When you run on visual studio on debug mode when those settings saving it will take some time. Go to debug folder and run the exe you will get the messages as expected.