Posted By

pckujawa on 03/04/10


Tagged

c gui Net csharp persistence


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

dbuksbaum


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


 / Published in: C#
 

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

Read the best article on the subject - Exploring Secrets of Persistent Application Settings - and then the following links if desired. (Note, I recommend reading this forum post about settings not being upgraded.)

It might help to understand What is app.config for?

Also of note: Saving out a Form's Size and Location using the Application Settings feature. 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.

  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. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: pckujawa on April 22, 2010

I'm skeptical about the 'UpgradeSettings' boolean bit, since it doesn't seem to work for one of the applications that I'm working with. I'm not sure that I've ever seen it work correctly, actually.

Posted By: pckujawa on April 23, 2010

It looks as though the upgrade method doesn't generally work with ClickOnce. See all the problems people have posted about from this google search.

Posted By: pckujawa on April 23, 2010

Someone figured out a way to get it to work with ClickOnce, though - http://bytes.com/topic/visual-basic-net/answers/854235-my-settings-upgrade-doesnt-upgrade

You need to login to post a comment.