16

I would like to override the use of the standard app.config by passing a command line parameter. How do I change the default application configuration file so that when I access ConfigurationManager.AppSettings I am accessing the config file specified on the command line?

Edit:

It turns out that the correct way to load a config file that is different than the name of the EXE plus .config is to use OpenMappedExeConfiguration. E.g.

ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "Shell2.exe.config");
currentConfiguration = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);

This partially works. I can see all of the keys in the appSettings section but all the values are null.

5 Answers 5

14

So here is the code that actually allows me to actually access the appSettings section in a config file other than the default one.

ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "Alternate.config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);

AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
string MySetting = section.Settings["MySetting"].Value;
0
3

A batch file that copies your desired configuration file to appname.exe.config and then runs the appname.exe.

4
  • Thanks for all of the support guys and gals. While you may not like it, it works. Lately, my approach is a -c=file.config and a generic console app and an Darrel's approach. I still have the generic console app copy the theapp.exe.config file to theapp.exe, to fool .NET.
    – kenny
    Commented Feb 25, 2010 at 13:56
  • 1
    This is the only answer (including the accepted one) that actually achieves the original desired result in any way. Commented Aug 26, 2011 at 15:39
  • This solution has several disadvantages. The most important one: As far as I know an application should not modify any files below the Program Files folder. So what sense does it make to place the config file next to the exe file in general? The application then is not able to modify it's own configuration. It is in general a good style to place the application's configuration under the home directory of the user. However, Windows still lacks of an central configuration file directory like /etc in order to place system-wide configuration files.
    – realtime
    Commented Sep 29, 2014 at 15:19
  • All comments have good arguments. This is not the most elegant solution but in some situations it can work. Let's say your app is not installed under programs and you cannot deploy a new version with the "OpenMappedExeConfiguration" fix. Someone in a company needs a quick run with a different config. A .bat and some edit of the .config files and you are done. Of course this is suitable for intranet apps with an IT support team, not for general release.
    – Luca
    Commented Oct 14, 2015 at 8:47
1

I needed to do this for an app of mine as well, and dealing with the standard config objects turned into such a freakin' hassle for such a simple concept that I went this route:

  1. Keep multiple config files in XML format similar to app.config
  2. Load the specified config file into a DataSet (via .ReadXML), and use the DataTable with the config info in it as my Configuration object.
  3. So all my code just deals with the Configuration DataTable to retrieve values and not that craptastically obfuscated app config object.

then I can pass in whatever config filename I need on the command line and if one isn't there - just load app.config into the DataSet.

Jeezus it was sooo much simpler after that. :-)

Ron

1
  • I hear you. I keep waiting for the day when I realize, oh that's the advantage of using .config files. That day has not come. Commented Oct 2, 2008 at 19:44
1

This is not exactly what you are wanting... to redirect the actual ConfigurationManager static object to point at a different path. But I think it is the right solution to your problem. Check out the OpenExeConfiguration method on the ConfigurationManager class.

If the above method is not what you are looking for I think it would also be worth taking a look at using the Configuration capabilities of the Enterprise Library framework (developed and maintained by the Microsoft Patterns & Practices team).

Specifically take a look at the FileConfigurationSource class.

Here is some code that highlights the use of the FileConfigurationSource from Enterprise Library, I believe this fully meets your goals. The only assembly you need from Ent Lib for this is Microsoft.Practices.EnterpriseLibrary.Common.dll.

static void Main(string[] args)
{
    //read from current app.config as default
    AppSettingsSection ass = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings;

    //if args[0] is a valid file path assume it's a config for this example and attempt to load
    if (args.Length > 0 && File.Exists(args[0]))
    {
        //using FileConfigurationSource from Enterprise Library
        FileConfigurationSource fcs = new FileConfigurationSource(args[0]);
        ass = (AppSettingsSection) fcs.GetSection("appSettings");
    }

    //print value from configuration
    Console.WriteLine(ass.Settings["test"].Value);
    Console.ReadLine(); //pause
}
5
  • The weird thing about this example is that it will load the default configuration file. If I change the exePath to be something else the OpenExeConfiguration fails. Commented Oct 2, 2008 at 18:40
  • I am going to get you a code sample in the next 20 minutes or so, but I went ahead and updated with a potential alternative. Commented Oct 2, 2008 at 19:04
  • Ironically, this application is actually using CAB from the P&P group however, I have extracted all EntLib stuff as it is overkill for me. I'm seriously thinking about stuffing an XML file in the folder. It would be so much easier. Commented Oct 2, 2008 at 19:25
  • All you need is one EntLib assembly, you don't need the full thing and you don't need any extra configuration (which is pretty typical of other aspects of Ent Lib). Commented Oct 2, 2008 at 19:32
  • Thanks for your effort, but if I can avoid taking a dependency on Entlib (V1, 2, 3, 3.1, 4, etc) I am going to. Commented Oct 2, 2008 at 19:51
1

This is the relevant part of the source for app that uses default config and accepts override via command line:

Get current or user config into the Config object

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string defCfgName = Environment.GetCommandLineArgs()[0] + ".config";

if (arg.Length != 0)
{
    string ConfigFileName = arg[0];
    if (!File.Exists(ConfigFileName))
        Fatal("File doesn't exist: " + ConfigFileName, -1);                
    config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = ConfigFileName }, ConfigurationUserLevel.None);
}
else if (!File.Exists(defCfgName)) Fatal("Default configuration file doesn't exist and no override is set." , -1);

Use the config object

AppSettingsSection s = (AppSettingsSection)config.GetSection("appSettings");
KeyValueConfigurationCollection a = s.Settings;
ConnectionString = a["ConnectionString"].Value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.