There is a load of great config stuff out on the net, for a full explaination of whats going on and how to use it try Jon Rista's superb set of articles on CodeProject.
I find the simplest way of doing basic config is to use AppSettingReader along with an App.Config file.
Add a new item to the project, select Application Configuration and an App.Config file will be added to the solution with very little in it:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
For this example, I wanted to store a set of numbers that would be used to determine how many samples to take from a set of data. So a comma seperated string will do the trick. "10,5,3"
The AppSettingReader will look for an AppSettings node in the config file, so:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
</configuration>
And a value is stored in KeyValue pair node:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SampleCount" value="10,5,3" />
</appSettings>
</configuration>
My setting item is called "SampleCount" and the value is........ well, there to read :-)
To read the value, create an instance of AppSettingsReader and use the GetValue method to look for the setting by Key:
//read number of samples to take from each file
AppSettingsReader asr = new AppSettingsReader();
String[] sampleCounts = (asr.GetValue("SampleCount", typeof(string)).ToString()).Split(new Char[]{ ',' });
The config file is available for anyone to edit so its best to be a bit defensive when using the values
Int32 sampleCount = 0;
Int32.TryParse(sampleCounts[i], out sampleCount);
No comments:
Post a Comment