GAC 中程序集的配置文件

发布于 2024-07-08 23:45:01 字数 124 浏览 4 评论 0原文

我有一个 .NET dll,它需要从它的配置文件中读取它的配置设置。 通常,配置文件与 DLL 放在同一目录中。 但是,如果 DLL 是 GAC 的,我如何读取配置文件,因为我只能将 DLL 放入 GAC 中,而不能放入配置文件。

I have a .NET dll which needs to read it's config settings from it's config file. Usually, the config file is placed in the same directory as the DLL. But how do i read the config file if the DLL is GAC'ed, because I can put only the DLLs in the GAC, and not it's config files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度℉ 2024-07-15 23:45:01

用户需要配置Dll吗? 如果是这样,那么 DLL 应该使用 app.config 文件中的配置设置,而不是它自己的配置。 app.config 文件应与应用程序存储在同一目录中。 如果没有,那么您可以采取几种不同的方式。 您可以更改 machine.config 文件,以便您的 Dll 可以在那里找到它们。 我不会这样做。 或者,您可以将配置存储在设置类中。 这些可以通过配置覆盖,但您的默认值将通过属性在设置类的生成代码中设置,因此当需要默认值时,缺少配置文件不会影响您的 Dll。

Does the user need to configure the Dll? If so, then the DLL should be using configuration settings from the app.config file, not it's own config. The app.config file should be stored in the same directory as the application. If not, then you could go a couple of different ways. You could make changes to the machine.config file so that your Dll can find them there. I would not do this. Alternatively, you can store the configuration in a settings class. These can be overridden via configuration, but your defaults will be set in the generated code for the settings class via attributes and so the absence of a configuration file will not affect your Dll when the defaults are all that are required.

听风吹 2024-07-15 23:45:01

我同意 tvanfosson 的观点,Gac'ed dll 将从应用程序的路径中读取。
但您也可以通过以下方式告知 dll 路径:

System.Configuration.ExeConfigurationFileMap fileMap = new System.Configuration.ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "THE PATH TO THE CONFIG";
System.Configuration.Configuration cfg =
System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);

string thevalue=cfg.AppSettings.Settings[variable].Value;

I agree with tvanfosson the Gac'ed dll will read from the application's path.
But you could also inform the dll which is the path in this way:

System.Configuration.ExeConfigurationFileMap fileMap = new System.Configuration.ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "THE PATH TO THE CONFIG";
System.Configuration.Configuration cfg =
System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);

string thevalue=cfg.AppSettings.Settings[variable].Value;
从﹋此江山别 2024-07-15 23:45:01

您可以使用 AppDomain.CurrentDomain.BaseDirectory ,因为 DLL 库不会自行执行,您只需要获取调用他的可执行文件目录,

例如:

var appDomain = AppDomain.CurrentDomain.BaseDirectory;
string sFileName = appDomain.Replace("\\bin\\Debug", "");
sFileName = sFileName + "Config\\config.xml";

这里我的可执行文件位于文件夹 bin\Debug 并在文件夹内有一个名为 Config 的文件夹,其中包含 xml 配置文件 config.xml。 因此,sFileName将为您提供配置文件的相对路径,如\bin\Debug\Config\config.xml

You can make use of AppDomain.CurrentDomain.BaseDirectory since the DLL library will not be executed by itself you just need to get Executable file directory who is calling him

Something like:

var appDomain = AppDomain.CurrentDomain.BaseDirectory;
string sFileName = appDomain.Replace("\\bin\\Debug", "");
sFileName = sFileName + "Config\\config.xml";

Here my executable is in folder bin\Debug and inside the folder i have a folder called Config where i have the xml config file config.xml. So the sFileName will provide you the relative path to the config file as \bin\Debug\Config\config.xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文