How do I select a .Net application configuration file from a command line parameter?

前端 未结 5 1433
失恋的感觉
失恋的感觉 2021-01-01 19:20

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 a

相关标签:
5条回答
  • 2021-01-01 19:24

    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:

    1. Keep multiple config files in XML format similar to app.config
    2. Load the specified config file into a DataSet (via .ReadXML), and use the DataTable with the config info in it as my Configuration object.
    3. So all my code just deals with the Configuration DataTable to retrieve values and not that craptastically obfuscated app config object.

    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

    0 讨论(0)
  • 2021-01-01 19:26

    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 the OpenExeConfiguration method on the ConfigurationManager 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 is 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
    }
    
    0 讨论(0)
  • 2021-01-01 19:30

    So here is the code that actually allows me to actually access the appSettings section in a config file other than the default one.

    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;
    
    0 讨论(0)
  • 2021-01-01 19:32

    A batch file that copies your desired configuration file to appname.exe.config and then runs the appname.exe.

    0 讨论(0)
  • 2021-01-01 19:40

    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

    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);
    

    Use the config object

    AppSettingsSection s = (AppSettingsSection)config.GetSection("appSettings");
    KeyValueConfigurationCollection a = s.Settings;
    ConnectionString = a["ConnectionString"].Value;
    
    0 讨论(0)
提交回复
热议问题