如何从命令行参数中选择.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部分中看到所有键,但是所有值均为null。
解决方案
批处理文件,它将所需的配置文件复制到appname.exe.config,然后运行appname.exe。
这不完全是我们想要的...重定向实际的" ConfigurationManager"静态对象以指向其他路径。但是我认为这是解决我们问题的正确方法。在ConfigurationManager
类上签出OpenExeConfiguration
方法。
如果上述方法不是我们要寻找的方法,那么我认为使用Enterprise Library框架的配置功能(由Microsoft Patterns&Practices团队开发和维护)也值得一看。
具体来看一下" FileConfigurationSource"类。
这里有一些代码突出显示了Enterprise Library中FileConfigurationSource
的用法,我相信这完全可以满足目标。 Ent Lib唯一需要的程序集是" 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 }
我也需要为我的一个应用程序执行此操作,并且对于这样一个简单的概念,处理标准的配置对象变得如此烦恼,我选择了以下方法:
- 以类似于app.config的XML格式保留多个配置文件
- 将指定的配置文件加载到DataSet中(通过.ReadXML),并将其中包含配置信息的DataTable用作我的Configuration对象。
- 因此,我所有的代码都只处理Configuration DataTable以检索值,而不是那种令人费解的应用程序配置对象。
那么我就可以在命令行中输入所需的任何配置文件名,如果没有,只需将app.config加载到DataSet中。
耶祖斯,那太简单了。 :-)
罗恩
因此,以下代码实际上使我可以实际访问默认文件以外的配置文件中的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;
这是使用默认配置并通过命令行接受替代的应用程序源的相关部分:
将当前或者用户配置获取到Config对象中
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;