C# dll 的 App.config

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/154837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:58:50  来源:igfitidea点击:

App.config for dll

提问by daharon

We have an "engine" that loads dlls dynamically (whatever is located in a certain directory) and calls Workflow classes from them by way of reflection.

我们有一个“引擎”,它动态加载 dll(无论位于某个目录中的任何内容)并通过反射从它们调用 Workflow 类。

We now have some new Workflows that require access to a database, so I figured that I would put a config file in the dll directory.

我们现在有一些需要访问数据库的新工作流,所以我想我会在 dll 目录中放置一个配置文件。

But for some reason my Workflows just don't see the config file.

但出于某种原因,我的工作流程看不到配置文件。

<configuration>
  <appSettings>
      <add key="ConnectString" value="Data Source=officeserver;Database=mydatabase;User ID=officeuser;Password=officeuser;" />
  </appSettings>
</configuration>

Given the above config file, the following code prints an empty string:

鉴于上述配置文件,以下代码打印一个空字符串:

Console.WriteLine(ConfigurationManager.AppSettings["ConnectString"]);

I think what I want is to just specify a config filename, but I'm having problems here. I'm just not getting results. Anyone have any pointers?

我想我想要的只是指定一个配置文件名,但我在这里遇到了问题。我只是没有得到结果。有人有任何指示吗?

采纳答案by MusiGenesis

If your code sample for reading the AppSettings is in your DLL, then it will attempt to read the config file for the application and not the config file for the DLL. This is because you're using Reflection to execute the code.

如果用于读取 AppSettings 的代码示例位于 DLL 中,那么它将尝试读取应用程序的配置文件,而不是 DLL 的配置文件。这是因为您正在使用反射来执行代码。

回答by Joel Coehoorn

Funny, where I'm at we're doing something very similar and the config file loads just fine. In our case I think each new config file's name matches that of it's associated assembly. So MyLibrary.dll would have a file named MyLibrary.dll.config with information for that file assembly. Also, the example I have handy is using VB.Net rather than C# (we have some of each) and all the settings in there are for the VB-specific My.Settings namespace, so we don't use the ConfigurationManager class directly to read them.

有趣的是,我所在的地方我们正在做一些非常相似的事情,并且配置文件加载得很好。在我们的例子中,我认为每个新配置文件的名称都与其关联程序集的名称相匹配。因此 MyLibrary.dll 将有一个名为 MyLibrary.dll.config 的文件,其中包含该文件程序集的信息。此外,我手头的示例是使用 VB.Net 而不是 C#(我们每个都有一些),并且那里的所有设置都是针对 VB 特定的 My.Settings 命名空间的,因此我们不直接使用 ConfigurationManager 类来阅读它们。

The settings themselves look like this:

设置本身如下所示:

<applicationSettings>
    <MyLibrary.My.MySettings>
        <setting name="SomeSetting" serializeAs="String">
            <value>12345</value>
        </setting>
    </MyLibrary.My.MySettings>
</applicationSettings>

回答by Dana

If I recall correctly, the app.config will be loaded from your application directory, so if you are loading dlls from some other directory, you'll want the keys they need in your application's config file.

如果我没记错的话,app.config 将从您的应用程序目录加载,因此如果您从其他目录加载 dll,您需要在应用程序的配置文件中包含它们所需的密钥。

回答by Juan Zamudio

I'm not totally sure but I think that class only works with the path of the entry method of the AppDomain (the path of the exe most of the time) by default. You need to call OpenExeConfiguration(string exePath) (Framework 2.0 and later) first to point to a different config file.

我不完全确定,但我认为默认情况下,该类仅适用于 AppDomain 的入口方法的路径(大多数情况下是 exe 的路径)。您需要首先调用 OpenExeConfiguration(string exePath)(Framework 2.0 及更高版本)以指向不同的配置文件。

回答by Mike Dimmick

I wrote this for a similar system. My recollection is that I used Assembly.GetExecutingAssemblyto get the file path to the DLL, appended .configto that name, loaded it as an XmlDocument, navigated to the <appSettings>node and passed that to a NameValueSectionHandler's Createmethod.

我为类似的系统写了这个。我记得我曾经Assembly.GetExecutingAssembly获取 DLL 的文件路径,附加.config到该名称,将其加载为XmlDocument,导航到<appSettings>节点并将其传递给 aNameValueSectionHandlerCreate方法。

回答by 0xDEAD BEEF

Here is one way - AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file");

这是一种方法 - AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file");

Call in constructor.

调用构造函数。