Click here to Skip to main content
11,711,537 members (80,877 online)
Click here to Skip to main content

How To Use the Settings Class in C#

, 15 Feb 2007 CPOL 316.6K 98
Rate this:
Please Sign up or sign in to vote.
A simple way to learn and start using the C# Settings class

Introduction

The first time I tried to store application settings using the Settings class that came with C#, I got stuck because the changed settings that I saved did not show up the next time I ran the application. I tried several times and eventually had to include another class into the project to accomplish saving the application state.

Recently while writing a project, I thought of how I would save the application settings and load it the next time the application is run. I didn't want to use my method for saving and loading the settings to and from disk - I wanted something ready-made. So the Settings class came to my mind again. Remembering my first experience with the class, I just wanted to chicken out, but due to the importance of saving the application's settings, I had to try it once more.

If I didn't make it, I would not have written this article. So enjoy it.

myApp.Properties.Settings

I'll walk you through the process of setting your program ( "myApp" ) to use the Settings class.

Click on the Project menu -> myApp Properties.

A new tab spawns showing you the various properties that can be set or read in the application you are currently building.

Click the Settings tab in the list of properties.

Enter the Settings you'd like to add (see the image below).

Settings Property page

When you type the name, you select the type, then you set the scope before you enter the value. You will have to take note of the Scope property. That is, in what 'scope' do you want the setting to be visible, is it at the user level (visible only to the current user) or at the application level (visible to the application from all user accounts). The logic is that if the scope is Application, once set you cannot change it from outside the IDE - you sure wouldn't want anything to tamper with what makes your program 'sane'. If the scope is User then it can be changed at runtime and the new value will be stored for you - which you can then read another time the program is run. This is exactly what most of us need from this class, no more no less. Once you're finished writing the name and setting the properties and values (make sure there is a default value you give to each name you 'declare'), click the Save button.

Using PostTracker.Properties.Settings

The main use of the Settings class comes up in your code. Let's assume the application we're working on is PostTracker and we have created the following settings names using the Settings Properties page:SmtpHost, Account, Password, Port.

Reading PostTracker.Properties.Settings

If you have created a usable setting that you want to save to disk, then it must be readable from the disk. Well, you don't have to worry about reading the settings from disk, it's automatically done for you, but you will need to get it from where it is stored. To do this, check out the code below:

account = PostTracker.Properties.Settings.Default.Account;
host = PostTracker.Properties.Settings.Default.smtpHost;
password = PostTracker.Properties.Settings.Default.Password;
port = Convert.ToString( PostTracker.Properties.Settings.Default.Port );

Don't ever forget to have declared your variables and make sure they are visible in the code block before you use them. You also need to note that the responsibility to convert to the appropriate type when reading or saving rests on you.

You might be tempted to read this way;

// No Compile time or runtime error will be generated at this line
// but account will hold no value
account = Properties.Settings.Default.Account;

Writing PostTracker.Properties.Settings

The following lines of code demonstrate how to save your settings to disk:

PostTracker.Properties.Settings.Default.Account = account;
PostTracker.Properties.Settings.Default.smtpHost = host;
PostTracker.Properties.Settings.Default.Password = password;
PostTracker.Properties.Settings.Default.Port = Convert.ToInt16( port );
PostTracker.Properties.Settings.Default.Save( ); // Important for saving

You must remember to call the Save() method if you really mean to keep your application settings.

Additional Information

Maybe you plan to put up a Settings page for your application and you're thinking of how to write code that checks to see if one of the settings has been changed by the user so you make the "Apply" button active for the user or decide it's not yet time to make a particular button active on the form. Well, you'll only have to write a few lines of code.

Sample Settings Form

This code is only a guide. Similar code you write should be wired to the appropriate events from your controls.

// The content of the TextBox that holds the user's password has just been changed
textboxPassword_TextChanged(object sender, EventArgs e)
{
    if(PostTracker.Properties.Settings.Default.Password || textboxPassword.Text
        // . . . check for other properties can ( and should ) come in here
        )
        {
            // Your action comes here, for example
            UpdateApplyButton();
        }
}
// You can wire other related events form related controls to the Method to save time

Conclusion

In conclusion, it's pretty simple and interesting to use the Settings class in your apps, but not knowing how to use it might just put you off the little treasure.

I really hope you enjoy this article. If you have questions or suggestions or corrections or ..., you're most welcome to send me an email.

Thank you.

History

  • 15th February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Seun
Web Developer TEDATA
Nigeria Nigeria
Seun (Laolusrael) loves programming. I learnt 80% of what I know in programming all alone. I use my God-given mind to do a lot of thinking, and...
Currently Seun is a student studying software engineering, and working freelance.

You may also be interested in...

Comments and Discussions

 
GeneralThanks Pin
Member 667631-Aug-12 20:41
memberMember 667631-Aug-12 20:41 
GeneralSave settings in C# Pin
Ank_ush14-Aug-12 9:00
memberAnk_ush14-Aug-12 9:00 
GeneralRe: Save settings in C# Pin
dkroy30-Aug-12 7:55
memberdkroy30-Aug-12 7:55 
GeneralMy vote of 5 Pin
Markus-8118-Aug-12 21:28
memberMarkus-8118-Aug-12 21:28 
GeneralMy vote of 4 Pin
vivekvolt10-Jun-12 20:43
membervivekvolt10-Jun-12 20:43 
Questionupdate settings file whenever there is a change in app.config Pin
BradNorthA18-Apr-12 20:44
memberBradNorthA18-Apr-12 20:44 
GeneralMy vote of 4 Pin
wkiess0120-Mar-12 14:48
memberwkiess0120-Mar-12 14:48 
Questionvery good and clear article Pin
nazari_ci26-Dec-11 6:49
membernazari_ci26-Dec-11 6:49 
QuestionThank you, Pin
bennee17-Dec-11 12:03
memberbennee17-Dec-11 12:03 
GeneralMy vote of 5 Pin
sumon67312-Sep-11 15:25
membersumon67312-Sep-11 15:25 
QuestionPerfect! Pin
LimeMan10-Aug-11 15:56
memberLimeMan10-Aug-11 15:56 
GeneralWow! Thanks so much! Pin
BBrown12711-Nov-10 4:00
memberBBrown12711-Nov-10 4:00 
QuestionCorrections? Pin
Sam Hobbs23-Jul-10 8:40
memberSam Hobbs23-Jul-10 8:40 
AnswerRe: Corrections? Pin
GhanashyamL6-May-11 8:55
memberGhanashyamL6-May-11 8:55 
GeneralRe: Corrections? Pin
Sam Hobbs6-May-11 9:24
memberSam Hobbs6-May-11 9:24 
GeneralChecksum for Settings Pin
StefanS8329-Jun-10 3:07
memberStefanS8329-Jun-10 3:07 
GeneralGreat article! Pin
_mns26-Apr-10 0:24
member_mns26-Apr-10 0:24 
GeneralExcellent article, thanks! Pin
Mohammad Amoo12-Mar-10 4:26
memberMohammad Amoo12-Mar-10 4:26 
GeneralExcellent article, thanks! Pin
LMoody25-Feb-10 5:09
memberLMoody25-Feb-10 5:09 
GeneralProblem with "Properties.Settings.Default" Pin
dkalyan20-Feb-09 2:21
memberdkalyan20-Feb-09 2:21 
GeneralRe: Problem with "Properties.Settings.Default" Pin
joehinder20-Mar-09 13:27
memberjoehinder20-Mar-09 13:27 
GeneralRe: Problem with "Properties.Settings.Default" Pin
viperdh13-May-09 4:32
memberviperdh13-May-09 4:32 
GeneralConfiguration system failed to initialize Pin
boss-tech15-Dec-08 3:29
memberboss-tech15-Dec-08 3:29 
GeneralCan't assign value - read only Pin
MrFox13-Jul-08 21:13
memberMrFox13-Jul-08 21:13 
GeneralRe: Can't assign value - read only Pin
MrFox15-Jul-08 21:59
memberMrFox15-Jul-08 21:59 
GeneralRe: Can't assign value - read only Pin
LMoody25-Feb-10 5:13
memberLMoody25-Feb-10 5:13 
GeneralRe: Can't assign value - read only Pin
aralele6-Dec-11 23:19
memberaralele6-Dec-11 23:19 
QuestionMissing the point? Pin
nickvh26-Jun-08 16:49
membernickvh26-Jun-08 16:49 
GeneralBack to registry comparrison Pin
tlhintoq20-Apr-08 14:22
membertlhintoq20-Apr-08 14:22 
GeneralRe: Back to registry comparrison Pin
penguin_chris27-Nov-08 3:35
memberpenguin_chris27-Nov-08 3:35 
GeneralStill I cannot get it working (Save issue) Pin
Member 41714382-Apr-08 1:17
memberMember 41714382-Apr-08 1:17 
GeneralRe: Still I cannot get it working (Save issue) Pin
Member 41714382-Apr-08 3:17
memberMember 41714382-Apr-08 3:17 
AnswerRe: Still I cannot get it working (Save issue) Pin
Seun2-Apr-08 6:25
memberSeun2-Apr-08 6:25 
QuestionApplication based setting - Webservice location Pin
dvius20-Feb-08 19:05
memberdvius20-Feb-08 19:05 
AnswerRe: Application based setting - Webservice location Pin
Seun1-Mar-08 0:48
memberSeun1-Mar-08 0:48 
QuestionHow to save a inputed numerical value Pin
Member 460216825-Jan-08 9:22
memberMember 460216825-Jan-08 9:22 
AnswerRe: How to save a inputed numerical value Pin
Seun27-Jan-08 12:50
memberSeun27-Jan-08 12:50 
QuestionSaving custom objects Pin
GroXx16-Sep-07 8:05
memberGroXx16-Sep-07 8:05 
GeneralBrilliant Pin
D Strauss9-Jul-07 3:44
memberD Strauss9-Jul-07 3:44 
GeneralRe: Brilliant Pin
Seun27-Jan-08 12:53
memberSeun27-Jan-08 12:53 
QuestionSettings values Pin
DannyAdler8-May-07 1:35
memberDannyAdler8-May-07 1:35 
AnswerRe: Settings values Pin
Seun9-May-07 14:08
memberSeun9-May-07 14:08 
AnswerRe: Settings values Pin
DannyAdler9-May-07 22:08
memberDannyAdler9-May-07 22:08 
GeneralRe: Settings values Pin
Seun10-May-07 13:54
memberSeun10-May-07 13:54 
GeneralGreat Tip - Good explaination Pin
buyValu7-May-07 7:27
memberbuyValu7-May-07 7:27 
Generalregistry Pin
riscy21-Feb-07 19:10
memberriscy21-Feb-07 19:10 
GeneralRe: registry Pin
Seun23-Feb-07 11:00
memberSeun23-Feb-07 11:00 
GeneralRe: registry Pin
Matware26-Feb-07 15:56
memberMatware26-Feb-07 15:56 
GeneralRe: registry Pin
Seun15-Mar-07 23:42
memberSeun15-Mar-07 23:42 
GeneralRe: registry Pin
Taneth4-Dec-07 8:31
memberTaneth4-Dec-07 8:31 

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
Web03 | 2.8.150819.1 | Last Updated 15 Feb 2007
Article Copyright 2007 by Seun
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid