A Low Maintenance XML Settings Mechanism - CodeProject



I needed an easy way to edit and exchange the config settings of a Windows service using a Windows Forms application.

As the .NET settings are really a hassle to accomplish something like this, I decided to write a simple XML settings class and a "SettingsManager" class that uses XML serialization to write and read the settings to a .config file.

Using the Code

Only the Forms application (see the demo, VS2013) is included here, as discussing Windows services is out of the scope of this tip, and it is enough to demonstrate the principle.

The nice thing about the Form is that it does not need any editing when the XML Settings class has changed.
Reflection is used to extract the settings class straight into a grid:


// Convert mySettings to grid.
foreach (var prop in SettingsManager.mySettings.GetType().GetProperties())
    string propValue = prop.GetValue(SettingsManager.mySettings, null).ToString();
    this.bindingSource1.Add(new ProvItem { Name = prop.Name, Value = propValue });

dataGridView1.DataSource = this.bindingSource1;
dataGridView1.Columns[0].ReadOnly = true;

Points of Interest

Note: If you are using an obfuscator, you will probably need to exclude the settings Form from obfuscation.

The generated settings .config file can also be viewed and edited with a normal text editor, e.g. Notepad++.

Happy serializing!

Software developer in the Netherlands, currently working on Video Surveillance applications.
Experience: C#, C++, VB, ASP, SQL Server, PostgreSQL.
It all started with Black&White, no not the whiskey but the Sinclair ZX81 followed by several Atari's and PC's. The journey continues ...