reading external configuration file

I have a c# .Net console app that performs FTP operations. Currently, I specify the settings in a custom configuration section, e.g.

<?xml version="1.0" encoding="utf-8" ?>
    <section name="ftpConfiguration" type="FileTransferHelper.FtpLibrary.FtpConfigurationSection, FileTransferHelper.FtpLibrary" />

      <Environment name="QA">
        <sourceServer hostname="QA_hostname"
                      remoteDirectory ="QA_remoteDirectory" />
        <targetServer downloadDirectory ="QA_downloadDirectory" />



I would like to specify, in the command line, an external configuration file.


I just realized that the above “FtpConfiguration” section doesn’t truly belong in the application’s app.config. My ultimate goal is that I will have many scheduled tasks that execute my console app like this:

FileTransferHelper.exe -c FtpApplication1.config
FileTransferHelper.exe -c FtpApplication2.config
FileTransferHelper.exe -c FtpApplication99.config

Consequently, I believe I’ve gone down the wrong path and what I really want is something to read in my custom xml document but continue to use System.Configuration to get the values… as opposed to reading an XmlDocument and serializing it to get nodes/elements/attributes. (Although, I’m not against the latter if someone can show me some simple code)

Pointers would be greatly appreciated. Thanks.

Update: The answer I accepted was a link to another StackOverflow question, repeated here with my code – below which was exactly what I was looking for — using the OpenMappedExeConfiguration to open my external config file

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = @"D:DevelopmentFileTransferHelperConfigurationSampleInterface.config";

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

FtpConfigurationSection ftpConfig = (FtpConfigurationSection)config.GetSection("ftpConfiguration");


If you want to use System.Configuration to open your custom files, you may want to check on this post: Loading custom configuration files. Oliver nails it in a very straightforward way.

Since you want to read parameters passed to your application via the command line, you may want to pay a visit to this MSDN post: Command Line Parameters Tutorial.

If you’d rather use a custom approach, there’s a few ways you can accomplish this. One possibility is to implement a loader class, and consume your custom configuration files.

For example, let’s assume a simple config file that looks like this:


<?xml version="1.0" encoding="utf-8"?>
    <add key="hostname" value="QA_hostname" />
    <add key="username" value="QA_username" />

A very simple, hashtable-like (key-value pair) structure.

An implemented parser/reader would then look something like this:

        private Hashtable getSettings(string path)
            Hashtable _ret = new Hashtable();
            if (File.Exists(path))
                StreamReader reader = new StreamReader
                    new FileStream(
                XmlDocument doc = new XmlDocument();
                string xmlIn = reader.ReadToEnd();
                foreach (XmlNode child in doc.ChildNodes)
                    if (child.Name.Equals("Settings"))
                        foreach (XmlNode node in child.ChildNodes)
                            if (node.Name.Equals("add"))
            return (_ret);

Meanwhile, you’ll still be able to use ConfigurationManager.AppSettings[] to read from the original app.config file.