.NET 自己的配置文件 [英] .NET own configuration file
问题描述
有什么方法可以在运行时指定我想使用的配置文件(App.config 除外)?例如,我想从命令行读取第一个参数,该参数将是应用程序配置的路径,并且我希望我的应用程序在使用 ConfigurationManager.AppSettings 时引用它(这可能是不可能的,但仍然值得一问).
我确实找到了这段代码:
Is there any way that I could specify at runtime the configuration file I would like to use (other than App.config)? For example I would like to read a first argument from a command line that will be a path to the application's config and I would like my application to refer to it when I use ConfigurationManager.AppSettings (It's probably impossible but still it's worth asking).
I did find this piece of code:
System.Configuration.Configuration config
= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = myRuntimeConfigFilePath;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
它可以工作,但它覆盖了原始 App.config 的 AppSettings 部分,并且我的应用程序不应写入任何内容.
It works, but it overrides the original App.config's AppSettings section and my application isn't supposed to write anything.
推荐答案
我发现了这个并且它有效.path"是配置文件的路径.
I found this and it works. "path" is a path to configuration file.
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
这篇关于.NET 自己的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!