C#Dot Net Perls

 
Visual Studio Settings.settings Use

by Sam Allen
Settings in Visual Studio.

Problem

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. You want to learn about the built-in settings features in .NET to make persisting data easier.

Solution

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 was a smart move and helps improve speed of development and the feature set of programs.

How do I get started?

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.

  1. Select the file
    Double click on the Settings.settings file and you should see a settings table.
  2. Use table
    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.
  3. Select application or user
    Select whether it is an application setting or a user setting (see below). This indicates whether the setting will be changed by the user or not.
Settings in Visual Studio.

What does scope mean?

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 Category Meaning
Application Not changed by the user; constant in every instance of the app
User Value that is not constant throughout each instance

How can I use these values in my 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 SavedInputString 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;

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. Neat!
    Properties.Settings.Default.Save();
}

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 "ExpireMs" item
// was manually added to the settings file in the table in Visual Studio.
long expireTime = Properties.Settings.Default.ExpireMs;

Conclusion

The beauty of this approach is that the resources are automatically turned into regular properties. This is very useful for developing applications quickly. Finally, it might be more resource-efficient to cook all our own meals and grow our own food, but using other people's convenience methods, such as Settings.settings, allows us to pursue other tasks.

Dot Net Perls is dedicated to sharing code and knowledge. It has
© 2007-2008 Sam Allen. All rights reserved.

Ads by The Lounge