C# Settings.settings in Visual Studio

by Sam Allen - Updated November 27, 2009

You want to use the Settings.settings file in Visual Studio in your C# .NET program. Writing a setting to disk when a program exits and then reading it back when the program is started again is cumbersome. Here we see examples and background material for the Settings.settings file in Visual Studio and .NET.

Settings.settings file being used in Visual Studio

Understanding Settings.settings

In .NET, 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.

Using Settings.settings in VS

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.

Using the Scope column

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.

Scope:   Application
Meaning: Not changed by the user.
         It is constant in every instance of the application.


Score:   User
Meaning: The setting is not constant throughout each instance.
         It is reset for each user installation.

Using Settings in C# 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.

//
// 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 on code example. 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.

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.

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

Summary

Here we saw ways to use the Settings.settings file in Visual Studio with C#. The resources are automatically turned into regular properties. This is very 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.

(Do not copy this page.)

Dot Net Perls | Search
Visual Studio | Visual Studio 2008 Review | Visual Studio Debugging Tutorial | Visual Studio Post-Build, Pre-Build Macros | Visual Studio Tab Options | Visual Studio TODO Comments
C# | SaveFileDialog Tutorial | IntegralHeight Property (Windows Forms) | Array.FindIndex Method | File.Replace Method
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen