我想通过传递命令行参数来覆盖使用标准的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键的部分,但所有值都为空。
因此,这里是真正让我真正访问appSettings部分在非默认的其他配置文件中的代码。
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, "Alternate.config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
string MySetting = section.Settings["MySetting"].Value;
一个批处理文件,拷贝所需的配置文件来appname.exe.config,然后运行appname.exe。
我需要为我的应用程序也这么做,并处理标准配置对象变成了这样一个刻着麻烦了这样一个简单的概念,我去这条路线:
- 保留多个配置文件类似的app.config XML格式
- 加载指定的配置文件到DataSet(通过.ReadXML),并使用数据表与它的配置信息为我的配置对象 。
- 所以我所有的代码只是配置DataTable的交易来获取价值并不算craptastically混淆的应用程序配置对象。
然后我就可以通过在任何配置文件名,我需要在命令行上,如果一个不存在-只加载的app.config到DataSet。
Jeezus这是SOOO后要简单得多。 :-)
罗恩
这是使用默认配置,并经由命令行接受覆盖的源应用程序的相关部分:
获取当前或用户配置为配置对象
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string defCfgName = Environment.GetCommandLineArgs()[0] + ".config";
if (arg.Length != 0)
{
string ConfigFileName = arg[0];
if (!File.Exists(ConfigFileName))
Fatal("File doesn't exist: " + ConfigFileName, -1);
config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = ConfigFileName }, ConfigurationUserLevel.None);
}
else if (!File.Exists(defCfgName)) Fatal("Default configuration file doesn't exist and no override is set." , -1);
使用配置对象
AppSettingsSection s = (AppSettingsSection)config.GetSection("appSettings");
KeyValueConfigurationCollection a = s.Settings;
ConnectionString = a["ConnectionString"].Value;
这不正是你想要什么......实际重定向ConfigurationManager
的静态对象点不同的路径。 但我认为这是你的问题的解决方案。 退房OpenExeConfiguration
的方法ConfigurationManager
类。
如果上面的方法是不是你在找什么,我认为这也将是值得考虑看看使用配置能力的企业库框架(开发由微软模式与实践团队维护)。
具体来看看该FileConfigurationSource
类。
下面是一些代码,突出使用的FileConfigurationSource
从企业库 ,我认为这完全符合你的目标。 你耳鼻喉科库需要为这个唯一的组件Microsoft.Practices.EnterpriseLibrary.Common.dll
。
static void Main(string[] args)
{
//read from current app.config as default
AppSettingsSection ass = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings;
//if args[0] is a valid file path assume it's a config for this example and attempt to load
if (args.Length > 0 && File.Exists(args[0]))
{
//using FileConfigurationSource from Enterprise Library
FileConfigurationSource fcs = new FileConfigurationSource(args[0]);
ass = (AppSettingsSection) fcs.GetSection("appSettings");
}
//print value from configuration
Console.WriteLine(ass.Settings["test"].Value);
Console.ReadLine(); //pause
}
文章来源: How do I select a .Net application configuration file from a command line parameter?