Configuration

time to read 2 min | 253 words

Here it is, guys. My configuration manager.
I really likes this syntax:

string ConStr = ConfigurationSettings.AppSettings["ConnectionString"];

But this is read-only application-wide settings, it doesn't help you if you need to update the values, or want to use both application wide and user specific settings.
For this, I designed my own configuration manager, usage is really simple:

string ConStr = Configuration.AppSettings["ConnectionString"];

So far, exactly like you would've done normally, the real power is in the following statements:

if(Configuration.CanSetAppSettings)
   Configuration.AppSettings["ConnectionString"] = ConStr;
string Title = Configuration.UserSettings["Title"];
Configuration.UserSettings["HomePage"] = HomePageUri.ToString();

You can set the application settings, and you get user specific settings also settable.
Considering that as a normal user you might not be able to make machine-wide changes, you also get the CanSetAppSettings property, that tells you whatever you are allowed to do so.

The settings are preserved as XML files in the following directories:
Application settings:
 %CommonApplicationData%\<you application name\<major version>.<minor version>\<your application name>.config

User settings:
 %ApplicationData%\<you application name\<major version>.<minor version>\<your application name>.config

You can also get the file names programmatically using the UserFile and the AppFile properties.

You can iterate over the settings using the following syntax:

foreach(string key in Configuration.UserSettings)
   Console.WriteLine(Configuration.UserSettings[key]);

There is a tiny optamization for when you set a lot of settings in one go, to avoid saving them one by one, the actual saving is done tenth of second after the last setting, or at application shut-down.

Curently the only limitation is that it can handle strings only (similar to the FCL's ConfigurationSettings). If there will be demand, I will add the ability to serialize objects directly from the Configuration class.

Get it here.

Enjoy.