Programmatically Updating App.config in Console Applications

Do you remember ini files?  They seemed so easy to use in the old days.  I think what was most appealing about them was the ease with which you could read and write to them either programmatically or in a text editor.  I still use this simple configuration pattern for console applications where a full database is too much to setup and I don’t want to use the registry because it stores settings in a different place than the exe file.  Below are some notes on how I use the .net ConfigurationManager class to accomplish this.

Reading Configurations

Reading configurations works the same way for console, WinForm, and asp.net apps.  They all automatically load either the app.config or web.config associated with the application.  Below is a sample appSettings section of a config file.

<appSettings>
    <add key=”lastRunTime” value=”8/16/2011 10:05:20 PM”/>
</appSettings>

To access the values you make use of the ConfigurationManager class.  To get access to the ConfigurationManager please make sure you add a reference to your project for System.Configuration and the following using statement.

using System.Configuration;

Once those two items are in place you can start accessing items in your config file with the following syntax.

namespace AppConfigExample
{
    class Program
    {
        static void Main(string[] args) {
            DateTime lastRunTime = DateTime.Parse(ConfigurationManager.AppSettings[“lastRunTime”]);
        }
    }
}

This returns the string value in your config file so you may need to cast it depending on the datatype you want to work with.  For example our lastRunTime is a DateTime so I could cast the value as follows.

DateTime lastRunTime = DateTime.Parse(ConfigurationManager.AppSettings[“lastRunTime”]);  //please add error handing to production code

This gives you a simple tool to store basic configurations for your program.  On to the next step…

Updating and Saving Configurations

In the previous example we had a setting for last run time.  If this setting is going to be correct we will need to update the config file every time the application runs.  Below is the code that will load the application configuration into a variable you can work with, update the lastRunTime setting to the current date, and save the file.

namespace AppConfigExample
{
    class Program
    {
        static void Main(string[] args) {
            Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Environment.GetCommandLineArgs()[0]);
            appConfig.AppSettings.Settings[“lastRunTime”].Value = DateTime.Now.ToString();
            appConfig.Save(ConfigurationSaveMode.Modified);
        }
    }
}

OpenExeConfiguration needs the exe name for the application so it can pull the correct config file.  A generic way to get the exe name is to use Environment.GetCommandLineArgs()[0].  That way if you rename your application your code will continue to work.

Summary

These two options should give you what you need to read and write simple configurations for your console applications.  The config files for .net do have more information than a traditional ini file, but if you stick to working with the appSettings section you should not run into any issues.