Using the Settings file in C#

Windows Forms

SettingsAs a software developer, how many times have you needed to store your application’s settings in a file, be it an ini file, an xml file, or maybe even a plain text file? It is a common requirement for many developers, and with C# it is really simple to do.

The Microsoft.NET Framework 2.0 introduced a new item called the Settings File. This file is added to your project by default and can be found under the Properties folder within Visual Studio’s Solution Explorer. The default name for this file is Settings.settings and it contains an easy to use interface for adding your application settings at design time.

Settings File Properties

A settings file contains the following four properties:

  • Name – The Name property is the name that is used to access the value of the setting through your code at runtime.
  • Type – The Type is the .NET Framework type of the setting like for example bool, decimal, int, and string. It even supports more complex types such as System.Drawing.Color, or System.Guid.
  • Scope – This property specifies how a setting can be accessed at runtime. It can be one of two values – either User or Application. The main difference between these two is that Application settings are read-only at runtime, while user settings are not. User settings should be used to load/save user preferences such as for example colour settings or window positions. Application settings should be used to store settings required for your application to run such as connection strings for example.
  • Value – The value property is the actual value that will be returned when accessing the setting at runtime.

Creating a Settings File

We are going to create a settings file with three settings – TextColour, Width, and Height. Open the Settings.settings file and add these three properties as shown below:


The TextColour property’s scope is set to User, therefore we will be able to update this property’s value through code at runtime. The other two properties are read-only.

Using the Settings File through Code

Create a simple form which looks like the below image with two buttons and a label for the text.

Settings Demo Form

In the Click event for the ‘Resize Window’ button we are going to add some code which reads the Width and Height settings and applies them to our form:

private void btnResize_Click(object sender, EventArgs e)
    // Load the width and height from the settings file 
    // and assign them to the form.
    this.Width = Properties.Settings.Default.Width;
    this.Height = Properties.Settings.Default.Height;

Now in the Click event for the ‘Change & Save Colour’ button we are going to add code which sets the label’s text colour to our TextColour property in the settings file. Then we are going to update the TextColour property with a new colour – which is blue. The code for this is below:

private void btnChangeColour_Click(object sender, EventArgs e)
    // Set the label's text colour to the colour 
    // we have in the setting file (red)
    lblText.ForeColor = Properties.Settings.Default.TextColour;

    // Set the colour in the settings file to blue.            
    Properties.Settings.Default.TextColour = Color.Blue;

If you run the application we just created and click on both buttons you will end up with a form which looks similar to this:

Settings Demo Form

As you can see the setting we loaded from our settings file have been applied to our form. If you run the application again and click the ‘Change & Save Colour’ button again the text will turn blue instead of red.

As you can see, storing application and user settings is extremely easy and straight forward to implement with C# and .NET 2.0.

I hope you found this article interesting. Stay tuned for more real soon.
Dave runs on the Thesis Theme

How smart is your Theme? How good is your support? Check out ThesisTheme for WordPress.

The Thesis Theme is one of the best Wordpress frameworks out there, especially if you understand software development, which since you're here you probably do. Thesis allows you to design post and page templates using a drag-and-drop system which gives you access to all the relevant page elements you would normally have to manually access through code. This is why understanding development techniques definitely helps, even though you don't need to write any code to get Thesis up and running.

So go check out the incredible features at DIYThemes and start using Thesis now!

5 comments… add one
  • Waqar

    Hi Dave,
    Many thanks for this article, its very useful. specially the way you have explained.
    Now I am going to read all your articles.


  • Fawad Naseer

    Dear Dave,

    I have seen some great articles on C# on your website, it’s simple and easy to understand, so good work for making it so simple.

    I don’t see that much resource on C# Accounting, if you can help me with that, is there any module available online, or from somewhere it can be purchased?.

    please let me know if you have such information.

    Fawad Naseer.

  • Paul Harder

    I stumbled across this article while searching for an answer to a somewhat different question. You may know the answer: When I create a new project in Visual Studio and open the settings file to define application configuration settings, the default Scope is always “User”. Is there any way to make Scope default to “Application”?


Leave a Comment