Sunday, April 1, 2007


Back in the VB6 days, when creating applications that required specific settings unique to a particular machine (such as screen sizes, fonts or even connection strings), I used to use either an INI file or the registry for storing these settings.

A few days ago, when writing an application on .Net v2, I needed to store similar settings and it had me thinking; should I use an INI file, the registry or even an XML file. The logical answer seemed to be to use an XML. So, I started searching the internet for possible solutions.

For the better part of half-an-hour I went through a number of articles, knowledge bases, forums and blogs and then it struck me. A pure and simple answer on the Microsoft MSDN forums ( Why not use My.Settings?

When using My.Settings, you are actually saving the settings in the app.config file in XML format. Simply double click on 'My Project', select the 'Settings' tab and enter the settings you require for your application. Remember these settings are unique for the local machine and shall NOT be available on any other machine.

Now, when adding new settings, you need to add a name for the setting, which cannot include spaces. You need to select the type, which is the data type. You also need to specify the scope. For this you have 2 settings, either 'User' which means that this setting is a 'read-write' setting, or 'Application' which simply means that this is a 'read-only' setting. You also need to specify a value.

Now, in code, whenever you need to read a particular setting then simply use My.Settings to access the setting value. You can also save a new value to pre-defined setting. Lost? Well, so was I at the beginning; so lets show an example. Lets assume we have 2 settings, named 'Username' and 'Password' (you would NOT usually save the username and password in a settings file; this is only for demonstrational purposes). Lets also assume that we have a textbox called 'tbUsername' and another called 'tbPassword'.

In code, to read the settings into the textboxes you would simply write:

tbUsername.Text = My.Settings.Username
tbPassword.Text = My.Settings.Password

Now, assuming the user changes these and you'd like to store the new values, you'd write:

My.Settings.Username = tbUsername.Text
My.Settings.Password = tbPassword.Text

and then you'd type:


Read more here and here