Persisting values in an application between executions and upgrades (.NET)


/ Published in: C#
Save to your folder(s)

Read the [best article on the subject - Exploring Secrets of Persistent Application Settings](http://www.devx.com/dotnet/Article/33944/1954) - and then the following links if desired. (Note, I recommend reading [this forum post about settings not being upgraded](http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/790980e8-eb89-492c-976b-4f93dc7c1caf/).)

It might help to understand [What is app.config for?](http://stackoverflow.com/questions/1431742/what-is-app-config-for)

Also of note: [Saving out a Form's Size and Location using the Application Settings feature](http://blogs.msdn.com/rprabhu/archive/2005/11/28/497792.aspx). Be sure to read the first couple of comments as well.

Lastly, if you are interested in import/export functionality, check out [my post on the subject](http://stackoverflow.com/questions/2389290/how-to-load-a-separate-application-settings-file-dynamically-and-merge-with-curre).


Copy this code and paste it in your HTML
  1. // See URLs. Below is some of the important code.
  2.  
  3. // Save a toolstrip's information:
  4. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  5. ToolStripManager.SaveSettings(this);
  6. Properties.Settings.Default.Save();
  7. . . .
  8. }
  9.  
  10. private void MainForm_Load(object sender, EventArgs e)
  11. {
  12. ToolStripManager.LoadSettings(this);
  13. ...
  14. }
  15.  
  16. // To discard changes during the current session:
  17. Properties.Settings.Default.Reload();
  18.  
  19. // To restore settings to "out-of-the-box":
  20. Properties.Settings.Default.Reset();
  21.  
  22. // Maintaining settings between versions
  23. if (Properties.Settings.Default.UpgradeSettings)
  24. {
  25. Properties.Settings.Default.Upgrade();
  26. Properties.Settings.Default.UpgradeSettings = false;
  27. Properties.Settings.Default.Save();
  28. }
  29.  
  30. // You will also need to create the referenced setting (UpgradeSettings) and initialize it to True.
  31.  
  32.  
  33. // Saving a form's location and size
  34. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  35. {
  36. Properties.Settings.Default.MyState = this.WindowState;
  37. if (this.WindowState == FormWindowState.Normal)
  38. {
  39. Properties.Settings.Default.MySize = this.Size;
  40. Properties.Settings.Default.MyLoc = this.Location;
  41. }
  42. else {
  43. Properties.Settings.Default.MySize = this.RestoreBounds.Size;
  44. Properties.Settings.Default.MyLoc = this.RestoreBounds.Location;
  45. }
  46. Properties.Settings.Default.Save();
  47. }
  48. private void Form1_Load(object sender, EventArgs e)
  49. {
  50. this.Size = Properties.Settings.Default.MySize;
  51. this.Location = Properties.Settings.Default.MyLoc;
  52. this.WindowState = Properties.Settings.Default.MyState;
  53. }

URL: http://www.devx.com/dotnet/Article/33944/1954

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.