将 dll 的配置文件移动到调用该 dll 的应用程序 [英] moving the config files for a dll to the app that calls the dll

查看:18
本文介绍了将 dll 的配置文件移动到调用该 dll 的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有搜索功能的网络应用.搜索算法被编译成一个单独的dll.在搜索算法的 C# 代码中,我使用设置文件中保存的字符串指向搜索索引所在的目录.编译搜索代码后,设置信息将合并到 Search.dll.config 中,它与 Search.dll 一起放在 bin 目录中.现在,在我的 Web 应用程序中,我将 Search.dll 添加到引用中.配置文件未添加到 Web 应用程序中.但是,网络应用程序运行良好并且知道文件在哪里.因为在 Settings.Designer 内部,它使用 DefaultSettingValueAttribute 来分配默认值(如果配置文件不存在).

I have a web app that has search functionality. The search algorithm is compiled to a separate dll. In the C# code for the search algorithm, I am using strings held in a settings file to point to the directory where the search index resides. Once the search code is compiled, the settings info is incorporated in Search.dll.config which is put in the bin directory along with Search.dll. Now in my web app, I add Search.dll to the references. The config file is not added into the web app. However the web app runs fine and knows where the file is. Because inside Settings.Designer it uses the DefaultSettingValueAttribute to assign a default if the config file is not there.

我还如何将 Search.dll.config 添加到我的网络应用程序,以便操作员可以根据需要更改服务器上索引文件的位置?

How do I also add Search.dll.config to my web app so the operator can change the location of the index files on the server as need be?

谢谢

我尝试将配置文件添加到我的部署文件夹.但是 ASP.NET 将 dll 放在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root......的目录中,并且配置文件不会被复制到那里.所以此时我不知道如何在我的代码中包含配置文件.

I tried to add the config file to my deployment folder. But ASP.NET puts the dlls in a directory at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root...... and the config file doesn't get copied there. So at this point I have no idea how to include the config file with my code.

感谢您的帮助.

注意:

我一直在使用以下代码将配置文件的值导入应用程序.但是,依赖于dll和配置文件在同一个文件夹中,我不知道如何实现.

I have been using the following code to get the values of the config file into the app. However, it depends on the dll and the config file to be in the same folder, which I do not know how to accomplish.

    var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
    var config = ConfigurationManager.OpenExeConfiguration(location);
    var sections = config.Sections; //count of this is 21
    ConfigurationSectionGroup csg = config.GetSectionGroup("applicationSettings");
    ConfigurationSectionCollection csc = csg.Sections;
    ConfigurationSection cs = csc.Get("Search.Properties.Settings");

推荐答案

最好的办法是直接将配置添加到 Web 项目中..NET 并不真正支持以您尝试的方式与库关联的配置;这是设计使然.您库的其他用户可能需要不同的配置.将该文件标记为应复制到输出文件夹的内容.

Your best bet is to add the config to the web project directly. .NET doesn't really support configuration associated with a library in the way you are attempting; and this is by design. Other users of your library may need different configuration. Mark the file as being content that should be copied to the output folder.

为此,在文件属性中将构建操作"设置为内容"并将复制到输出目录"设置为如果更新则复制".您可以使用 HttpRuntime.BinDirectory 发现 bin 文件夹中的文件.您可能希望将此位置传递给您的库,而不是让库假设它正在网络项目中运行.

To do this set "Build Action" to "Content" and "Copy to Output Directory" as "Copy if newer" in the file properties. You can discover the file in the bin folder using HttpRuntime.BinDirectory. You may want to pass this location to your library rather than have the library assume it's running in a web project.

或者,只需在 web.config 中嵌入您需要的配置(配置文件也可以将设置分解为单独的文件).

Alternatively, just embed the config you need in web.config (config files have a facility to break out settings into a separate file as well).

最后,您可以考虑将文件嵌入为资源.

Finally, you could consider embedding the file as a resource.

看看你正在使用的代码,把它移到 web.config 中即可.更容易,也更地道.一旦它在 web.config 中,只需使用 ConfigurationManager.GetSection() 来读取您的部分.

Looking at the code you are using, just move it into web.config. Much easier, and idiomatic. Once it's in web.config just use ConfigurationManager.GetSection() to read your section.

顺便说一句,有一个免费的配置节设计器,可以非常轻松地创建支持自定义配置节的类.在此处查看 VS 扩展在线库:

Incidentally, there's a free configuration section designer that makes creating the classes to support custom configuration sections very easy. Look in the VS Extensions Online Gallery here:

http://visualstudiogallery.msdn.microsoft.com/2a69f74e-83df-4eb0-8cac-cd83b451cd1d?SRC=VSIDE

这篇关于将 dll 的配置文件移动到调用该 dll 的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆