GAC 中程序集的配置文件
我有一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用户需要配置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.
我同意 tvanfosson 的观点,Gac'ed dll 将从应用程序的路径中读取。
但您也可以通过以下方式告知 dll 路径:
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:
您可以使用 AppDomain.CurrentDomain.BaseDirectory ,因为 DLL 库不会自行执行,您只需要获取调用他的可执行文件目录,
例如:
这里我的可执行文件位于文件夹
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 himSomething like:
Here my executable is in folder
bin\Debug
and inside the folder i have a folder calledConfig
where i have the xml config fileconfig.xml
. So thesFileName
will provide you the relative path to the config file as\bin\Debug\Config\config.xml