利用自身的配置文件.NET 3.5 DLL [英] .NET 3.5 DLL using its own config file

查看:161
本文介绍了利用自身的配置文件.NET 3.5 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有一个.NET 3.5 DLL有它自己的配置文件。此DLL可以从许多不同的应用程序被调用,所以存储在所需要的配置文件中的信息(如连接字符串),以保持在该DLL可以引用一个配置文件。当DLL时,我需要开关,用于参考的信息是DLL的配置文件中的配置文件,我想是。然后,当该DLL与使用配置信息完成的,所述开关是由回缺省之一。该DLL是使用.NET 3.5编写。我一直在寻找如何做到这一点,我不断的发现是如何合并有EXE的app.config文件的信息。就我而言,我不知道该如何在那里此DLL将用于修改任何EXE的文件的app.config在那里。这个解决方案需要是独立的。然而,我的基类来创建DLL(包含业务对象)期待查找在配置文件中的连接字符串和其他信息,所以这就是为什么我要开关我的DLL的配置文件的时候,它被访问,然后再切换回来,所以我不要弄乱的EXE应用程序调用该DLL。

I need to have a .NET 3.5 DLL have its own config file. This DLL could be called from many different apps so the information (like connection strings) stored in the config file needed to be kept in a config file that the DLL can reference. What I want is when the DLL is used, I need to "switch" the config file that is used to reference information to be the DLLs config file. Then when the DLL is done with using the configuration information, the switch is made back to the default one. The DLL is written using .NET 3.5. I have been searching for how to do this and what I keep finding is how to merge information with an exe's app.config file. In my case, I don't know how where this DLL will be used to modify any exe's app.config files out there. This solution needs to be stand alone. However, my base classes used to create the DLL (which contain business objects) are expecting to lookup the connection string and other information in a config file so that is why I need to "switch" to my DLL config file at the time when it is accessed and then switch it back so I don't mess up the exe app that called the DLL.

推荐答案

在.NET 2.0及以上配置的系统给你的能力 - 例如,您可以按需加载一个特定的配置文件。这是一个有点更多的工作 - 但它的工作原理

The .NET 2.0 and up configuration system gives you capabilities - you can e.g. load a specific config file on demand. It's a bit more work - but it works.

您不得不做这样的事情:

You'd have to do something like this:

// set up a exe configuration map - specify the file name of the DLL's config file
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "ConfigLibrary.config";

// now grab the configuration from the ConfigManager
Configuration cfg = ConfigurationManager
                   .OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

// now grab the section you're interested in from the opened config - 
// as a sample, I'm grabbing the <appSettings> section
AppSettingsSection section = (cfg.GetSection("appSettings") as AppSettingsSection);

// check for null to be safe, and then get settings from the section
if(section != null)
{
   string value = section.Settings["Test"].Value;
}

您还需要确保配置的类库DLL正在兴建,并复制到那里你可以在它那里得到一个位置。最坏的情况下,你需要指定一个特定的配置文件,并通过设置其复制到输出目录属性在Visual Studio属性窗口确保它被复制到输出目录。

You also need to make sure that the config for the class library DLL is being built and copied to a location where you can get at it. Worst case you need to specify a specific config file and make sure it gets copied to the output directory by setting its "Copy To Output Directory" property in the Visual Studio properties window.

您也应该检查出乔恩Rista的三部系列.NET 2.0的配置上$ C $的CProject。

You should also check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

  • Unraveling the mysteries of .NET 2.0 configuration
  • Decoding the mysteries of .NET 2.0 configuration
  • Cracking the mysteries of .NET 2.0 configuration

强烈推荐,写得很好,非常有帮助!

Highly recommended, well written and extremely helpful!

马克·

这篇关于利用自身的配置文件.NET 3.5 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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