/ 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.
Expand |
Embed | Plain Text
// See URLs. Below is some of the important code. // Save a toolstrip's information: private void MainForm_FormClosing(object sender, FormClosingEventArgs e) ToolStripManager.SaveSettings(this); Properties.Settings.Default.Save(); . . . } private void MainForm_Load(object sender, EventArgs e) { ToolStripManager.LoadSettings(this); ... } // To discard changes during the current session: Properties.Settings.Default.Reload(); // To restore settings to "out-of-the-box": Properties.Settings.Default.Reset(); // Maintaining settings between versions if (Properties.Settings.Default.UpgradeSettings) { Properties.Settings.Default.Upgrade(); Properties.Settings.Default.UpgradeSettings = false; Properties.Settings.Default.Save(); } // You will also need to create the referenced setting (UpgradeSettings) and initialize it to True. // Saving a form's location and size private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Properties.Settings.Default.MyState = this.WindowState; if (this.WindowState == FormWindowState.Normal) { Properties.Settings.Default.MySize = this.Size; Properties.Settings.Default.MyLoc = this.Location; } else { Properties.Settings.Default.MySize = this.RestoreBounds.Size; Properties.Settings.Default.MyLoc = this.RestoreBounds.Location; } Properties.Settings.Default.Save(); } private void Form1_Load(object sender, EventArgs e) { this.Size = Properties.Settings.Default.MySize; this.Location = Properties.Settings.Default.MyLoc; this.WindowState = Properties.Settings.Default.MyState; }
Comments
Subscribe to comments
You need to login to post a comment.

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