Change default app.config at runtime

后端 未结 8 2065
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 05:50

I have the following problem:
We have an application that loads modules (add ons). These modules might need entries in the app.config (e.g. WCF configuration). Because t

8条回答
  •  逝去的感伤
    2020-11-22 06:47

    If your config file is just written with key/values in "appSettings", then you can read another file with such code :

    System.Configuration.ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
    configFileMap.ExeConfigFilename = configFilePath;
    
    System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
    AppSettingsSection section = (AppSettingsSection)configuration.GetSection("appSettings");
    

    Then you can read section.Settings as collection of KeyValueConfigurationElement.

提交回复
热议问题