Saving & Loading Application Settings in C#

If your application uses application settings that are the scope of “User”, in C# you will need to add code that will save and load these settings during shutdown and startup (this is automatic when using VB.NET).

In your Main() method of the Program.cs file add the first two lines below to load the settings and setup an event for saving:

[STAThread]
  static void Main()
  {
    HelloWorld.Properties.Settings.Default.Reset();
    Application.ApplicationExit +=
      new EventHandler(Application_ApplicationExit);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
  }

Then add this method to save the settings during application shutdown:

static void Application_ApplicationExit(object sender, EventArgs e)
{
  HelloWorld.Properties.Settings.Default.Save();
}

Tip By: David McCarter