Click here to Skip to main content
11,798,546 members (72,549 online)
Click here to Skip to main content

Windows Forms User Settings in C#

, 1 Aug 2006 290.5K 3.7K 129
Rate this:
Please Sign up or sign in to vote.
# programmers don’t have anything quite as slick as the VB.NET My.Settings namespace. However, there is an alternative


This morning, for the first time, I was jealous of VB.NET programmers. The My.Settings namespace makes loading and saving user settings pretty easy, and it’s pretty well documented. Unfortunately, C# programmers don’t have anything quite as slick as the My.Settings namespace, and the procedure for persisting user settings is not well-documented.

It turns out that it is pretty easy to persist user settings in C#, and this article will show you how to do it. We’re going to stick to the basics—nothing fancy. We will use as our example a Windows Forms “Hello World” program that does nothing but show a window with the canonical text:

We will create settings for the application window’s size and location properties, and we will persist them between runs of the program. The project code can be downloaded from the link at the top of this article.

Step One: Create the Settings

The easiest way to create settings is to use the Visual Studio Settings Designer. To get to the designer, open the project’s Properties pages, and select the Settings tab:

As you can see from the Settings grid, settings can have Application scope or User scope. Application settings are those that apply across all users, and that do not change from run to run of the program. Once an Application setting is set in the designer, it can’t be changed in code.

User settings are those that change from user to user, and from run to run of an application. They can be changed in code. User settings are commonly used to store user preferences, such as window size and location. That’s what we’re going to do here.

Fill out the Settings grid so that it looks like this:

The grid is self-explanatory. Note the default values in the Value column. I simply copied these from the Properties of the form. Note that you must include default values in the Values column! If you leave them out, the designer code will declare the settings, but it won’t instantiate them, and any code that calls them will throw a runtime exception.

Once you have entered the settings on the grid, save the project and close the Properties pages.

Step Two: Load the Settings at Runtime

The code to load the settings at runtime is very simple. The designer created a class for us that holds our settings. The class is located in a Properties namespace under our project name. Our first step is to add a using statement for this class:

using UserSettingsDemo.Properties;

Node that the Visual Studio code editor can add this statement for us automatically. If we haven’t added the statement, then the first time we type ‘Settings’, the code editor will present a smart tag that can enter the using statement automatically:

Once the using statement has been added, we simply need to add a few lines of code to the window’s event handlers.

Create a FormLoad event handler for the target window, and add the following code to the handler:

private void FormMain_Load(object sender, EventArgs e)
    // Set window location
    if (Settings.Default.WindowLocation != null)
        this.Location = Settings.Default.WindowLocation;

    // Set window size
    if (Settings.Default.WindowSize != null)
        this.Size = Settings.Default.WindowSize;

The code is self-explanatory. Note that we find our settings in a Properties class that the designer created for us. This class can hold multiple sets of settings for a user; we want the Default set.

Step Three Save the Settings at Runtime

Saving settings is almost as easy. However, there is a minor complication when saving a window’s size. We will see that in the code sample that follows.

The persistence code for settings is generally placed in the FormClosing event handler for the target window. Create the handler, and add the following code to it:

private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
    // Copy window location to app settings
    Settings.Default.WindowLocation = this.Location;

    // Copy window size to app settings
    if (this.WindowState == FormWindowState.Normal)
        Settings.Default.WindowSize = this.Size;
        Settings.Default.WindowSize = this.RestoreBounds.Size;

    // Save settings

The WindowLocation setting is self-explanatory, but the WindowSize property has a wrinkle. If a window is normal size, we can read its size property in the usual manner. But if a window is minimized or maximized, the size property will return an inaccurate value. So, .NET provides a RestoreBounds property that will return the size of the window in its normal state. But—and here’s the wrinkle—the RestoreBounds property returns a valid value only when the window is minimized or maximized. As a result, we have to test the WindowState property, and call either the Size or RestoreBounds property based on the results.

Note that we have to call the Settings’ Save() method to save the settings to the config file. And the config file is not the Application.exe.config file—only application settings go there. User settings are saved to different settings files for each user, which are stored in an arcane location. This FAQ explains how to find the settings file, in case you need to get to it.

Data Binding

Many settings can be data-bound from the Visual Studio Properties window. For example, we could have data bound our form’s location property (but not its size property) by setting a binding in the ApplicationSettings property:

The Size property can’t be data-bound because whether we use the Size or RestoreBounds property isn’t decided until runtime. But for many properties, data binding provides the simplest and easiest approach. Just remember that you will still need to save the Settings from code before your app closes:

// Save settings


And that’s about all there is to creating, loading, and saving user settings in C# 2.0. It’s just about as easy as using the VB.NET My.Settings namespace, and it provides a simple procedure for persisting just about any user setting you might create. And it means I can now go back to pretending I’m not jealous of some of the convenience features built in to VB.NET!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

David Veeneman
Software Developer (Senior) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Heaven20207-Aug-15 21:09
memberHeaven20207-Aug-15 21:09 
QuestionThank you Pin
Yahya Mohammed Ammouri19-May-15 21:29
memberYahya Mohammed Ammouri19-May-15 21:29 
GeneralIt works Pin
Axel Steiner25-Nov-14 1:18
memberAxel Steiner25-Nov-14 1:18 
GeneralExactly what I needed Pin
Member 846630719-May-14 16:12
memberMember 846630719-May-14 16:12 
QuestionNot keeping Settings Pin
Hafnium27-Mar-14 2:36
memberHafnium27-Mar-14 2:36 
AnswerRe: Not keeping Settings Pin
Fernando E. Braz8-Apr-14 16:52
memberFernando E. Braz8-Apr-14 16:52 
GeneralMy vote of 5 Pin
gicalle754-Dec-13 22:01
membergicalle754-Dec-13 22:01 
GeneralGreat & usefull Pin
Wrangly6-Nov-13 11:26
memberWrangly6-Nov-13 11:26 
GeneralMy vote of 5 Pin
Jayanta Chatterjee20-Aug-13 2:48
memberJayanta Chatterjee20-Aug-13 2:48 
QuestionAbout how settings are saved Pin
Xiao Lan13-Aug-13 0:14
memberXiao Lan13-Aug-13 0:14 
AnswerRe: About how settings are saved Pin
PandaNL13-Aug-13 1:16
memberPandaNL13-Aug-13 1:16 
GeneralRe: About how settings are saved Pin
Xiao Lan20-Aug-13 12:37
memberXiao Lan20-Aug-13 12:37 
QuestionMy vote of 5 Pin
Wusiji6-Jun-13 8:53
memberWusiji6-Jun-13 8:53 
QuestionUsing this on a multi instance application? Pin
SnapAidan7-Feb-13 0:14
memberSnapAidan7-Feb-13 0:14 
GeneralThank you! Pin
codecopy17-Nov-12 7:10
membercodecopy17-Nov-12 7:10 
GeneralMy vote of 4 Pin
ready to learn24-Feb-12 5:45
memberready to learn24-Feb-12 5:45 
Generalstore each form settings Pin
Marcel Vreuls ( 22:56
memberMarcel Vreuls ( 22:56 
GeneralMy vote of 5 Pin
Member 764112430-Mar-11 5:12
memberMember 764112430-Mar-11 5:12 
GeneralThank you. Pin
reynolds_john17-Sep-10 10:57
memberreynolds_john17-Sep-10 10:57 
GeneralExcellent.. Pin
Timothy Greaves30-Dec-09 8:17
memberTimothy Greaves30-Dec-09 8:17 
GeneralThank you Pin
dannygoh26-Jun-09 11:48
memberdannygoh26-Jun-09 11:48 
GeneralGetting System.NullReferenceException Pin
Craig C.17-Mar-09 5:39
memberCraig C.17-Mar-09 5:39 
GeneralSaving Setting OF ComboBox In UserConfig Pin
vikas Thukral4-Mar-09 23:02
membervikas Thukral4-Mar-09 23:02 
GeneralThank You Pin
fatveg8-Feb-09 17:22
memberfatveg8-Feb-09 17:22 
GeneralWindow Maximized Pin
Appy6613-Jan-09 8:40
memberAppy6613-Jan-09 8:40 
QuestionCan I do same changes in app.config Pin
akumarmishra22-Jul-08 3:48
memberakumarmishra22-Jul-08 3:48 
QuestionUnable to Update Pin
akumarmishra22-Jul-08 2:25
memberakumarmishra22-Jul-08 2:25 
GeneralPVidal - You're Right! Pin
cabby34521-Mar-08 14:50
membercabby34521-Mar-08 14:50 
QuestionNeed to use this.RestoreBounds.Location? Pin
PVidal20-Mar-08 17:57
memberPVidal20-Mar-08 17:57 
GeneralNice! Pin
hxkolo10-Oct-07 17:55
memberhxkolo10-Oct-07 17:55 
QuestionWhere it save the settings??? Pin
stefan.hm18-Sep-07 21:28
memberstefan.hm18-Sep-07 21:28 
AnswerRe: Where it save the settings??? Pin
hnun22-Sep-07 10:19
memberhnun22-Sep-07 10:19 
GeneralRe: Where it save the settings??? Pin
stefan.hm23-Sep-07 22:09
memberstefan.hm23-Sep-07 22:09 
GeneralSuperb Job Pin
Akajasimafat5-Sep-07 19:29
memberAkajasimafat5-Sep-07 19:29 
GeneralSmall Suggestion Pin
dfhgesart4-Jul-07 11:28
memberdfhgesart4-Jul-07 11:28 
GeneralReading the real "defaults" Pin
FocusedWolf25-Jun-07 7:00
memberFocusedWolf25-Jun-07 7:00 
AnswerRe: Reading the real "defaults" Pin
dfhgesart4-Jul-07 13:35
memberdfhgesart4-Jul-07 13:35 
GeneralGreat! Good job!! Pin
real yutian30-May-07 17:40
memberreal yutian30-May-07 17:40 
GeneralRe: Great! Good job!! Pin
David Veeneman31-May-07 11:19
memberDavid Veeneman31-May-07 11:19 
GeneralNeed Help Pin
Natarajasivan29-May-07 8:49
memberNatarajasivan29-May-07 8:49 
GeneralRe: Need Help Pin
David Veeneman29-May-07 9:26
memberDavid Veeneman29-May-07 9:26 
GeneralRe: Need Help Pin
Natarajasivan2-Jun-07 19:21
memberNatarajasivan2-Jun-07 19:21 
GeneralSystem.Dwawing.Size value problem Pin
Cmodijk27-Mar-07 0:33
memberCmodijk27-Mar-07 0:33 
GeneralRe: System.Dwawing.Size value problem Pin
--Simas--6-Apr-07 4:47
member--Simas--6-Apr-07 4:47 
GeneralQuick question about default location... Pin
Four13 Designs29-Jan-07 3:19
memberFour13 Designs29-Jan-07 3:19 
GeneralRe: Quick question about default location... Pin
David Veeneman29-Jan-07 11:32
memberDavid Veeneman29-Jan-07 11:32 
GeneralRe: Quick question about default location... Pin
Four13 Designs29-Jan-07 14:06
memberFour13 Designs29-Jan-07 14:06 
QuestionHow to save datetimepicker? Pin
Kfiry15-Jan-07 3:41
memberKfiry15-Jan-07 3:41 
QuestionHow do I store the contents of comboboxes Pin
Poolee26-Dec-06 11:05
memberPoolee26-Dec-06 11:05 
AnswerRe: How do I store the contents of comboboxes Pin
Vertyg026-Dec-06 12:13
memberVertyg026-Dec-06 12:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 1 Aug 2006
Article Copyright 2006 by David Veeneman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid