The ConfigurationManager class in C# has been one of my latest discoveries for storing all the configuration data required by an application. Here is one example of how I used it to store some of my configuration data:
Let us say that you have written an application (let’s call is Foo.exe). Now let us say that Foo.exe logs some information and it requires the log file location to be passed in as an input via configuration info.
So, here is what you could do.
1. Add an Application Config file to Foo.exe (You could do this on Visual Studio via Add > New Item > Application Configuration File). Let us call it Foo.config
2. Open Foo.config. It’ll look something like this:
<?xml version=“1.0“ encoding=“utf-8“ ?>
3. Add the following lines into Foo.config between the <configuration> </configuration> tags:
<add key=“LoggingFile“ value=“C:\Logs\Foo.log“ />
Where the key “ LoggingFile” is the key, querying the value of which will fetch you the log file path.
4. Now within your application, here is how you can fetch the value of “ LoggingFile“:
string logFilePath = ConfigurationManager.AppSettings[“LoggingFile“]
Wasn’t that easier than writing your own XML based config files and a parser to fetch the values from it? Hope this helps.