有什么方法可以在运行时指定我想使用的配置文件(App.config 除外)?例如,我想从命令行读取第一个参数,该参数将成为应用程序配置的路径,并且我希望我的应用程序在使用 ConfigurationManager.AppSettings 时引用它(这可能是不可能的,但仍然值得一问)。
我确实找到了这段代码:
System.Configuration.Configuration config
= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = myRuntimeConfigFilePath;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
它有效,但它覆盖了原始 App.config 的 AppSettings 部分,我的应用程序不应该写任何东西。