.NET 3.5 DLL使用自己的配置文件 [英] .NET 3.5 DLL using its own config file
问题描述
2.0及以上配置系统为您提供了一些功能 - 例如根据需要加载特定的配置文件。这是一个更多的工作 - 但它的工作。
你必须这样做:
//设置一个exe配置映射 - 指定DLL的配置文件的文件名
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename =ConfigLibrary.config;
//现在从ConfigManager中获取配置
配置cfg = ConfigurationManager
.OpenMappedExeConfiguration(map,ConfigurationUserLevel.None);
//现在从打开的配置抓取您感兴趣的部分 -
//作为示例,我抓住了< appSettings> section
AppSettingsSection section =(cfg.GetSection(appSettings)as AppSettingsSection);
//检查null是否安全,然后从
中获取设置if(section!= null)
{
string value = section.Settings [测试]值。
}
您还需要确保类库DLL的配置正在建立并复制到您可以获得的位置。最糟糕的情况是,您需要指定一个特定的配置文件,并确保它通过在Visual Studio属性窗口中设置复制到输出目录属性来复制到输出目录。
您还应该查看Jon Rista在CodeProject上的.NET 2.0配置的三部分系列。
强烈推荐,写得很好,非常有用!
Marc
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.
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;
}
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.
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!
Marc
这篇关于.NET 3.5 DLL使用自己的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!