16

我想通过传递命令行参数来覆盖标准 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 部分中的所有键,但所有值都是空的。

4

5 回答 5

14

所以这里的代码实际上允许我实际访问除默认配置文件之外的配置文件中的 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;
于 2008-10-02T19:48:27.420 回答
3

将所需配置文件复制到 appname.exe.config 然后运行 ​​appname.exe 的批处理文件。

于 2008-10-02T18:23:01.823 回答
1

这不完全是您想要的……将实际的ConfigurationManager静态对象重定向到不同的路径。但我认为这是解决您问题的正确方法。查看类OpenExeConfiguration上的方法ConfigurationManager

如果上述方法不是您想要的,我认为使用Enterprise Library 框架(由 Microsoft 模式和实践团队开发和维护)的配置功能也值得一看。

具体看一下FileConfigurationSource类。

下面是一些代码,重点介绍了FileConfigurationSourcefrom Enterprise Library的使用,我相信这完全符合您的目标。为此,您需要从 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
}
于 2008-10-02T18:27:26.587 回答
1

我也需要为我的应用程序执行此操作,并且处理标准配置对象变成了一个非常简单的概念,我走这条路:

  1. 以类似于 app.config 的 XML 格式保存多个配置文件
  2. 将指定的配置文件加载到DataSet中(通过 .ReadXML),并将其中包含配置信息的DataTable用作我的配置对象
  3. 所以我所有的代码都只处理配置数据表来检索值,而不是那个疯狂混淆的应用程序配置对象。

然后我可以在命令行上传入我需要的任何配置文件名,如果不存在 - 只需将 app.config 加载到DataSet中。

天哪,在那之后就简单多了。:-)

罗恩

于 2008-10-02T19:28:11.120 回答
1

这是使用默认配置并通过命令行接受覆盖的应用程序源的相关部分:

获取当前或用户配置到 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;
于 2009-07-07T11:56:22.767 回答