applicationSettings, appSettings. app.config, web.config and userSettings#

I have seen lots of posts from people in the newsgroups trying to figure out what the deal is with the new config options in .net 2.0.

The main reason people run into problems is due to MS making a number of changes, including changes between the beta and the RC product, resulting in different answers based on how early you encountered the issues.

You can still do things the way you did with .Net 1.x.

       <appSettings>

              <add key="MyKeyName" value="somevalue"/>

       </appSettings>

and then you can access it with:

Dim myvalue As String = System.Configuration.ConfigurationSettings.AppSettings.Item("ConnectionString")

However, if you try this, VS.Net will warn you:

This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings

It is important to note that the "!" above indicates that the fully quallified class listed, is located in the System.Configuration assembly, which of course you have to add to your project. 

So unlike previous versions, you need to manually add a reference to System.Configuration in order to make this new call.

Now, .net 2.0 and vs.net 2005 have teamed up to offer a new option for storing user settings which may seem more complicated at first if you don't know exactly what you are doing.  In the project properties you can select a "Settings" tab where you are able to modify application settings, which are in turn stored in app.config.  These settings can either be "User" settings, or "Application" settings. 

Application settings are stored in the app.config (or web.config), and are read-only.  User settings have a default value that is stored in the app.config, but your application can overwrite these default values as needed, and the users settings will be stored in:

%USERPROFILE%\Local Settings\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config. (non roaming)

OR:

%USERPROFILE%\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config (roaming)


The other nice feature of the 2.0 way of accessing these settings is that the settings are saved as a specific type from a wide collection of available types (String, System.DateTime, System.Drawing.Color, System.Guid, Bool, StringCollection etc) and when you access them from your code they are available in intellisense.

This may seem like it isn't important, but it means you can't mistype a setting key, or accidently try an invalid cast from one of your settings.  Also, storing a collection was a real pain in 1.x.    Now you can create a collection quickly and it will be added to the user settings like this:

   <setting name="MyCol" serializeAs="Xml">

        <value>

            <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xmlns:xsd="http://www.w3.org/2001/XMLSchema">

                <string>I am first</string>

                <string>second</string>

                <string>me third</string>

                <string>I am number four</string>

            </ArrayOfString>

        </value>

    </setting>

Pretty nice!

To access these properties you have 2 different ways depending on if you are using VB.Net or C#.

For VB.Net you use the "My" namespace and access it like this:

Dim mySetting As String = My.Settings.MySetting

For C# you access these settings through the "Properties" object

       string mySetting = Properties.Settings.Default.MyUserSetting;

That's it!

Categories:  |  |  |  | 
Wednesday, April 26, 2006 7:36:40 AM (Central Standard Time, UTC-06:00) #    Comments [9]  | 

 

Thursday, January 31, 2008 11:18:13 AM (Central Standard Time, UTC-06:00)
Thanks man!! this example helped me to go one step forward in my vs2005 journy.
Satyam
Thursday, January 31, 2008 11:23:25 AM (Central Standard Time, UTC-06:00)
No problem, thanks for the comment.
Tuesday, April 22, 2008 2:45:22 PM (Central Standard Time, UTC-06:00)
dude,

I read lot of app.config google results, yours made the sense.

Cheers
Tuesday, April 22, 2008 2:48:14 PM (Central Standard Time, UTC-06:00)
Thanks Paul
Monday, May 05, 2008 7:42:32 AM (Central Standard Time, UTC-06:00)
Thanks. I had spent some time trying to directly change the web.config file and auto complete wouldn't pick up the reference. After reviewing this, I change the project settings, it automatically added it to the web.config and i was done.

Thanks again.
Monday, May 05, 2008 8:41:05 AM (Central Standard Time, UTC-06:00)
No problem Joe. Thanks for leaving the comment.
Tuesday, May 20, 2008 1:13:08 AM (Central Standard Time, UTC-06:00)
Nice! Why couldn't anyone else explain it so clearly! So frustrated, was i, at the change from appSettings to applicationSettings that i wrote my own deserializing logic to read settings, i can now go back to settings files. Thanks.
Tuesday, May 20, 2008 9:26:41 AM (Central Standard Time, UTC-06:00)
Dave, thanks for the comment.

Someone must be linking to this article because I have been getting a lot of comments on this post lately.

Glad to help.
Wednesday, June 04, 2008 3:18:18 PM (Central Standard Time, UTC-06:00)
But how does one get the array of strings read out of the settings file in order to use in the application?
none
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

All content © 2008, Christopher May, Inc
Open Job Positions
On this page
Google Ads
This site
Calendar
<April 2006>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: