C# 是否可以在运行时为 .NET 应用程序切换应用程序配置文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/242568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:30:48  来源:igfitidea点击:

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

c#.netconfiguration

提问by Morgan Cheng

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.

默认情况下,.NET 应用程序的配置文件以“exe 文件名”.config 命名。我想知道是否可以动态指定一个应用程序的配置。

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”。在运行时,配置文件是“foo.exe.config”。是否可以让它接受命令行参数以使用其他配置文件。因此,应用程序可以使用其他配置,如下所示。

foo.exe /config:bar.config

foo.exe /config:bar.config

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

bar.config 代替 foo.exe.config 用作配置文件。

回答by Will Dieterich

Yes you will need use ExeConfigurationFileMap

是的,您将需要使用 ExeConfigurationFileMap

回答by Ray Lu

Code from MSDN

来自 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);
    }

}

回答by faulty

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

来自如何使用 Configuration.GetSection() 和 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;

回答by Slav

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

如果您只需要替换 AppSettings 部分,上述所有内容都可以正常工作。

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");