Settings.settings stores preferences on the disk. Writing a setting to disk when a program exits and then reading it back when the program is started again is cumbersome. We can use instead the Settings.settings file in Visual Studio and the .NET Framework.


In the .NET Framework, Microsoft introduced some mechanisms for programmers to easily store settings in a persistent way, meaning on the disk, and access it in memory whenever needed. This helps improve speed of development and the feature set of programs. It also maintains the concept of rapid application development.
This C# example uses the Settings.settings file in Visual Studio.
We get started through the Visual Studio C# 2005 GUI. In a typical Windows Forms project, there is a folder called Properties and a file called Settings.settings in that folder. Let's look at the steps you need to take to get this set up properly.
First steps to take. Select the file. Double click on the Settings.settings file and you should see a settings table. Next, type a name of the variable in the left-most column. Then, in the same row, set the type (int, string, etc.). Finally, set the value in the right side.
Understanding application/user. Select whether it is an application setting or a user setting. This indicates whether the setting will be changed by the user or not. Also, the user setting is reset by each user installation.

There is a Scope column in the table in Visual Studio. There are two options in each cell there: Application and User. Let's look at a useful table that shows each of the two scope values on the left, and their meanings on the right.
Application:
Not changed by the user.
It is constant in every instance of the application.
User:
The setting is not constant throughout each instance.
It is reset for each user installation.
The real bonus that C# and .NET provide in this situation is the ease of use of the syntax and the clarity of the required code. Here's what I do when I want to start up the program and retrieve the string and display it in a text box.
Code that loads settings string [C#] // // Read in a value from the Setting.settings file show in the above // screenshot. "SavedInputString" is just a custom variable which can // be named anything in your program. Set the Text of an input box // to the property. // inputBox.Text = Properties.Settings.Default.SavedInputString;
Notes. There are no complicated constructors or method calls, as .NET deals with all of that stuff. When the program is being exited, an event handler called FormClosing is called. Here's what I do to set the saved input string to the last input string.
Code that sets property in FormClosing [C#]
void TextWindow_FormClosing(object sender, FormClosingEventArgs e)
{
//
// We are going to write to the settings in our code. We take the results
// of a function, and set the settings string equal to it.
//
Properties.Settings.Default.SavedInputString = SanitizeInput(inputBox.Text);
//
// Now, we need to save the settings file. If we don't save it, they
// disappear. By saving it, we will be able to use all the settings exactly
// as they are now, the next time the program is run.
//
Properties.Settings.Default.Save();
}Note on FormClosing event. There is one method call that needs to be made when the program exits. That is easy to do, however, and making a simple FormClosing event handler is sufficient. I want to show next an example using a different data type, which is basically identical in usage. Here's how to retrieve a value from the settings.
Code that retrieves setting [C#] // // Retrieve a value from the Settings.settings file. The string // was manually added to the settings file in the table in Visual Studio. // long expireTime = Properties.Settings.Default.ExpireMs;

We saw ways to use the Settings.settings file in Visual Studio with the C# language. The resources are automatically turned into regular properties. This is useful for developing applications quickly. Using this convenient mechanism to persist application and user settings allows us to focus on the core of your Windows Forms application.
Visual Studio Tips