如何从命令行参数选择 .Net 应用程序配置文件?
我想通过传递命令行参数来覆盖标准 app.config 的使用。 如何更改默认应用程序配置文件,以便在访问 ConfigurationManager.AppSettings 时访问命令行上指定的配置文件?
编辑:
事实证明,加载与 EXE 加 .config 名称不同的配置文件的正确方法是使用 OpenMappedExeConfiguration。 例如,
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "Shell2.exe.config");
currentConfiguration = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);
这部分有效。 我可以看到 appSettings 部分中的所有键,但所有值均为空。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因此,这里的代码实际上允许我实际访问配置文件中的 appSettings 部分,而不是默认的部分。
So here is the code that actually allows me to actually access the appSettings section in a config file other than the default one.
一个批处理文件,将所需的配置文件复制到 appname.exe.config,然后运行 appname.exe。
A batch file that copies your desired configuration file to appname.exe.config and then runs the appname.exe.
我也需要为我的一个应用程序执行此操作,并且对于这样一个简单的概念,处理标准配置对象变得非常麻烦,所以我采用了这条路线:以
然后我可以在命令行上传入我需要的任何配置文件名,如果不存在 - 只需将 app.config 加载到 DataSet 中。
天啊,在那之后就简单多了。 :-)
罗恩
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:
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
这不完全是您想要的...重定向实际的
ConfigurationManager
静态对象指向不同的路径。 但我认为这是解决您问题的正确方法。 查看OpenExeConfiguration
方法ConfigurationManager
类。如果上述方法不是您想要的,我认为使用 企业库框架的配置功能(由 Microsoft 模式与实践团队开发和维护)。
具体请查看
FileConfigurationSource
类。下面是一些代码,重点介绍了 企业库,相信这完全符合您的目标。 为此,您需要从 Ent Lib 获取的唯一程序集是 Microsoft.Practices.EnterpriseLibrary.Common.dll。
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 theOpenExeConfiguration
method on theConfigurationManager
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 isMicrosoft.Practices.EnterpriseLibrary.Common.dll
.这是使用默认配置并通过命令行接受覆盖的应用程序源的相关部分:
将当前或用户配置获取到配置对象中
使用配置对象
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
Use the config object