Using application/user settings in C# 2.0

This is a quick post for using application or user settings in C# 2.0. I found most of the articles dealing with it in VB which is pretty easy.

All you need to access a setting called "ServerName" in Visual Basic is this line

My.Settings.ServerName="myServer"

For more information about settings you can refer to MSDN @ https://msdn2.microsoft.com/en-us/library/bc6ws923(VS.80).aspx (all samples in VB)

In C#, to access the application/user settings you need to use this line

String myserver=Properties.Settings.Default. myServer;

If you want to write to this

Properties.Settings.Default. myServer="myServer";

But take care that you can't change application settings in code. You can only change user settings. To change the application settings, you need to write your own code to open the configuration file as any xml file and modify it. I've written a simple component to do this which I'll post later this week.

Comments

  • Anonymous
    December 11, 2006
    "My"? What is it with "My"? I don't want to access my settings, I want to access the current user's settings! grumble it's almost as bad a "My Documents". If there's a folder called "my documents" on the computer and I didn't create it, I'm going to assume that someone else did. Therefore, it's not mine, it's "theirs".

  • Anonymous
    December 12, 2006
    My namespace is a new namespace in VB.NET 2005. It provides shortcuts for many operations. It's not only settings. For more information about it check http://msdn.microsoft.com/msdnmag/issues/05/07/My/ //Mohamed

  • Anonymous
    December 14, 2006
    This is an excellent article about how to use the new Settings feature in .NET 2.0. We needed to figure out how to get the current URL for a web-reference, and this makes it very easy. Thanks!

  • Anonymous
    December 14, 2006
    Adam, is there anything "My" could be called (e.g. "My Documents") to make you think it's yours if you didn't create it?  If it was called "User's Documents" and you didn't create it, would you think it was yours?

  • Anonymous
    January 27, 2007
    Just remember to call Properties.Settings.Default.Save(); after modifying user settings.

  • Anonymous
    July 29, 2007
    Hey Thanx! I got many article about setting but none of them was saying that settings is under property!!

  • Anonymous
    August 12, 2007
    When you give a code snippet .. please make sure to say which using statement to use.

  • Anonymous
    September 05, 2007
    full example of using setting:

  1. Insert new setting file, chose its name.
  2. in C# type NameOfSettingFile.Default. and you will see the settings items as a properties(note u must be in the same namespace as the setting file , you can change namespace in settings.designer file)
  • Anonymous
    December 09, 2007
    Thank you for this! I've been searching around and finding conflicting methods of handling user/application settings in .NET 2.0 / C# - none of the approaches were successful for me. Yours was... LM

  • Anonymous
    March 13, 2008
    Article is very much useful but it made me little curios that how we can change user access through code on local machine like read access, write acess etc. we have default users on machine like user@onThisPC ,Administrater@onThisPc.How can we edit their access. Thanks