我想通过传递命令行参数来覆盖标准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。

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

  1. 保留XML格式的多个配置文件,类似于app.config
  2. 将指定的配置文件加载到 DataSet (通过.ReadXML),并使用 DataTable 及其中的配置信息作为我的配置对象
  3. 所以我的所有代码都只处理 Configuration DataTable 来检索值,而不是那个可以混淆的app app对象。
  4. 然后我可以在命令行传递我需要的任何配置文件名,如果不存在 - 只需将app.config加载到 DataSet

    Jeezus之后那么简单得多了。 : - )

    罗恩

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

将当前或用户配置导入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;

这并不是你想要的......重定向实际的 ConfigurationManager 静态对象指向不同的路径。但我认为这是解决问题的正确方法。查看 OpenExeConfiguration 方法 ConfigurationManager 类。

如果上述方法不符合您的要求,我认为使用配置功能(由Microsoft Patterns& Practices团队开发和维护)。

具体看一下 FileConfigurationSource 类。

以下是一些代码,重点介绍 FileConfigurationSource 的用法-99BE-F8ECFBBC5B65&displaylang = en&Hash = oOfFascYgZ%2bhSk6bMSD0ctdKGJBo3jL9XEtSJS2%2bCaArLTPNHRCuJ5k%2bnhgG8YkFIMOdGq74qSBJRzpD6zppRg%3d%3d“rel =”nofollow noreferrer“>企业库,我相信这完全符合您的目标。您需要从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
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top