Click here to Skip to main content
6,596,602 members and growing! (19,324 online)
Email Password   helpLost your password?
Languages » C# » General     Beginner License: The Code Project Open License (CPOL)

How To Use the Settings Class in C#

By Seun

A simple way to learn and start using the C# Settings class
C# 2.0, Windows, .NETVS2005, Dev
Posted:15 Feb 2007
Views:66,374
Bookmarked:60 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
35 votes for this article.
Popularity: 6.80 Rating: 4.40 out of 5

1
1 vote, 2.9%
2
5 votes, 14.3%
3
5 votes, 14.3%
4
24 votes, 68.6%
5

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)

About the Author

Seun


Member
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.
Occupation: Web Developer
Company: Web-Mind
Location: Nigeria Nigeria

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 31 (Total in Forum: 31) (Refresh)FirstPrevNext
GeneralProblem with "Properties.Settings.Default" Pinmemberdkalyan3:21 20 Feb '09  
GeneralRe: Problem with "Properties.Settings.Default" Pinmemberjoehinder14:27 20 Mar '09  
GeneralRe: Problem with "Properties.Settings.Default" Pinmemberviperdh5:32 13 May '09  
GeneralConfiguration system failed to initialize Pinmemberboss-tech4:29 15 Dec '08  
GeneralCan't assign value - read only PinmemberMrFox22:13 13 Jul '08  
GeneralRe: Can't assign value - read only PinmemberMrFox22:59 15 Jul '08  
GeneralMissing the point? Pinmembernickvh17:49 26 Jun '08  
GeneralBack to registry comparrison Pinmembertlhintoq15:22 20 Apr '08  
GeneralRe: Back to registry comparrison Pinmemberpenguin_chris4:35 27 Nov '08  
GeneralStill I cannot get it working (Save issue) PinmemberMember 41714382:17 2 Apr '08  
GeneralRe: Still I cannot get it working (Save issue) PinmemberMember 41714384:17 2 Apr '08  
AnswerRe: Still I cannot get it working (Save issue) PinmemberSeun7:25 2 Apr '08  
QuestionApplication based setting - Webservice location Pinmemberdvius20:05 20 Feb '08  
AnswerRe: Application based setting - Webservice location PinmemberSeun1:48 1 Mar '08  
QuestionHow to save a inputed numerical value PinmemberMember 460216810:22 25 Jan '08  
AnswerRe: How to save a inputed numerical value PinmemberSeun13:50 27 Jan '08  
QuestionSaving custom objects PinmemberGroXx9:05 16 Sep '07  
GeneralBrilliant PinmemberD Strauss4:44 9 Jul '07  
GeneralRe: Brilliant PinmemberSeun13:53 27 Jan '08  
QuestionSettings values PinmemberDannyAdler2:35 8 May '07  
AnswerRe: Settings values PinmemberSeun15:08 9 May '07  
AnswerRe: Settings values PinmemberDannyAdler23:08 9 May '07  
GeneralRe: Settings values PinmemberSeun14:54 10 May '07  
GeneralGreat Tip - Good explaination PinmemberbuyValu8:27 7 May '07  
Generalregistry Pinmemberriscy20:10 21 Feb '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Feb 2007
Editor: Deeksha Shenoy
Copyright 2007 by Seun
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project