Wrong App.config being loaded

前端 未结 4 1972
无人及你
无人及你 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 19:52

    An app domain in C# can have only one assembly level app.config file. See here on MSDN. An executable will always start up an AppDomain and by default look for a config file with name: EXECUTABLE_NAME.config. For example, SampleApp01.exe will look for SampleApp01.exe.config as its configuration file.

    0 讨论(0)
  • 2021-01-13 19:53

    you can place your configs in the machine.config file inside the framework folder by this way you can globally use your configuration in all .Net applications running in that machine,

    0 讨论(0)
  • 2021-01-13 19:54

    I believe app.config will always be used by the executable. Just drop it in that directory.

    They would do that to ensure the dll can be shared and not have to share the same .config file.

    You might be able to create a link from the executable .config file

    <appSettings configSource="\lib\app.config">
    

    Or change its name, i don't understand how you can have both app.config files in the same directory..don't they have the same name?

    <appSettings configSource="\lib.app.config">
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题