Is it possible to switch application configuration file at runtime for .NET application?

10,437

Solution 1

All of the above work well if you need to replace only AppSettings section.

In case you have to run with different config files (all sections) you might want to consider launching application using a host, that creates app domain for your main application and sets different config file depending on parameters you passed in.

Here's the code that worked for me:

        AppDomainSetup setup = new AppDomainSetup();
        setup.ApplicationBase = "file://" + System.Environment.CurrentDirectory;
        setup.DisallowBindingRedirects = true;
        setup.DisallowCodeDownload = true;

        if (args.Length != 0 && args[0].Equals("-test"))
        {
            setup.ConfigurationFile = "PATH_TO_YOUR_TEST_CONFIG_FILE";
        }
        else {
            setup.ConfigurationFile = "PATH_TO_YOUR_LIVE_CONFIG_FILE";
        }

        AppDomain domain = AppDomain.CreateDomain("FRIENDLY_NAME", null, setup);
        domain.ExecuteAssembly("YourMainApp.exe");

Solution 2

Code from MSDN

static void DisplayMappedExeConfigurationFileSections()
{
    // Get the application configuration file path.
    string exeFilePath = System.IO.Path.Combine(
        Environment.CurrentDirectory, "ConfigurationManager.exe.config");
    // HERE !!!     
    // Map to the application configuration file.
    ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
    configFile.ExeConfigFilename = exeFilePath;

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

    // Display the configuration file sections.
    ConfigurationSectionCollection sections = config.Sections;

    Console.WriteLine();
    Console.WriteLine("Sections in machine.config:");

    // Loop to get the sections machine.config.
    foreach (ConfigurationSection section in sections)
    {
        string name = section.SectionInformation.Name;
        Console.WriteLine("Name: {0}", name);
    }

}

Solution 3

Gotten from How to use Configuration.GetSection() and ConfigurationManager.OpenMappedExeConfiguration()

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"C:\Inetpub\Test\Config\Dev.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
string ConfigVersion = section.Settings["ConfigVersion"].Value;
Share:
10,437

Related videos on Youtube

Morgan Cheng
Author by

Morgan Cheng

I am a software engineer in China. This is my Blog ??????????,??????

Updated on April 21, 2022

Comments

  • Morgan Cheng
    Morgan Cheng almost 2 years

    By default, .NET application's configuration file is named after "exe file name".config. I'm wondering whether it is possible to have one application's configuration specified dynamically.

    For example, the built application is "foo.exe". At runtime, the config file is "foo.exe.config". Is it possible to have it accept command line arguments to use other config file. So, the application can use other configuration like below.

    foo.exe /config:bar.config

    bar.config is used as config file insteand of foo.exe.config.

  • Morgan Cheng
    Morgan Cheng over 15 years
    can you give some details or reference to tutorials?
  • Will Dieterich
    Will Dieterich over 15 years
    Here is the MSDN example and documentation msdn.microsoft.com/en-us/library/…