Wrong App.config being loaded

前端 未结 4 1974
无人及你
无人及你 2021-01-13 19:12

I have a .NET 3.5 class library I built that reads an App.config file for values it needs. It can pull the config values just fine when I test it in Visual Studio. To test

4条回答
  •  走了就别回头了
    2021-01-13 20:13

    I can't find a way to avoid getting the app.config for the calling dll/exe etc. The only way I have found is to use a hardcoded path and load it that way. Here is code I am using to do that:

    using System.Configuration;
    ...
    public static KeyValueConfigurationCollection getAppSettingsFromAppConfig(String appConfigPath) {
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = appConfigPath;
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        AppSettingsSection section = config.AppSettings;
        KeyValueConfigurationCollection appsettings = section.Settings;
        return appsettings;
    }
    

    You then have a collection of KeyValueConfigurationElement, which you can use .Value to get the string from config file with.

提交回复
热议问题