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

前端 未结 5 1481
失恋的感觉
失恋的感觉 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: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;
    

提交回复
热议问题