Wrong App.config being loaded

断了今生、忘了曾经 提交于 2019-12-30 09:57:06

问题


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 it, I just change the project to a console application and execute a method call.

I have the need to call this class library from many other .NET programs, and I want the class library to be self sufficient (I should be able to call it from any other program, and it should use its own config file, not know about any calling config file etc.).

I can add a reference to the dll (since I am still development I am using VS 2008, haven't thrown anything into the GAC yet) but the App.config that the class library is reading is from the calling program's App.config, not the class library's App.config.

The class library dll has it's config file in the same directory, so it should be able to find it just fine, and the calling application is named differently. I am using the standard key value pairs in the App.config (e.g. name of config file myClassLibrary.dll.config) and getting values out with the following line of code:

String myVal = ConfigurationSettings.AppSettings["myConfigSetting"];

Does anyone know how to fix this?


回答1:


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.




回答2:


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,




回答3:


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">



回答4:


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.



来源:https://stackoverflow.com/questions/471042/wrong-app-config-being-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!