Return to Snippet

Revision: 24600
at April 23, 2010 09:51 by pckujawa


Updated Code
// 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;
}

Revision: 24599
at March 4, 2010 17:33 by pckujawa


Updated Code
// 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;
   }

// 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;
}

Revision: 24598
at March 4, 2010 17:33 by pckujawa


Updated Code
// 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;
   }

// 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;
}

Revision: 24597
at March 4, 2010 17:30 by pckujawa


Updated Code
// 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;
   }

// You will also need to create the referenced setting (UpgradeSettings) and initialize it to True.

Revision: 24596
at March 4, 2010 17:23 by pckujawa


Updated Code
See URLs.

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

Revision: 24595
at March 4, 2010 17:08 by pckujawa


Initial Code
See URLs.

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

Initial Description
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).

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

Initial Tags
c, Net

Initial Language
C#