C# Settings.settings in Visual StudioThis C# example uses the Settings.settings file in Visual Studio.
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.
Intro. There are some mechanisms for programmers to easily store settings on the disk, and access them in memory whenever needed. This helps improve speed of development and the feature set of programs. It makes programs faster to develop.
We get started
through the Visual Studio C# development environment. In a typical Windows Forms project, there is a folder called Properties and a file called Settings.settings in that folder.
Next: We set up settings. We look at the steps you need to take to get this properly configured.
First steps to take.
Select the file. Then, please double click on the Settings.settings file and you should see a settings table. Next, type a name of the variable in the leftmost column.
Then: In the same row, set the type (int, string). 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. Next, we describe the scope values available and their meanings.
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.
Code. 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;
There are no complicated constructors or method calls. When the program is being exited, an event handler called FormClosing is called. Here is how I 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.
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.
Next: I want to show an example using a different data type. 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.
Review: Using this convenient mechanism to persist application and user settings. It allows us to focus on the core of an application.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.