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.
<add key=“MyKeyName“ value=“somevalue“/>
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 SettingsApplication Data<Company Name><appdomainname>_<eid>_<hash><verison>user.config. (non roaming)
%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“>
<string>I am first</string>
<string>I am number four</string>
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;