从插件模块读取dll.config(不App.config的!) [英] Reading dll.config (not app.config!) from a plugin module
问题描述
我写一个C#.NET 2.0的.dll是一个插件到一个较大的应用程序。我的模块在Visual Studio项目有被复制到沿着MyProj.dll的一侧MyProj.dll.config一个app.config文件。
I am writing a C# .NET 2.0 .dll that is a plug in to a Larger application. The visual studio project for my module has a app.config file which is copied to a MyProj.dll.config along side of MyProj.dll.
该计划是该.dll展开后MyProj.dll.config将被编辑。我试图从该修改的本地文件读取我的设置。我试图拉出LocalFilesSettingsObject,改变它的应用程序名称,以我的.dll文件是这样的:
The plan is that MyProj.dll.config will be edited after the .dll is deployed. I am trying to read my settings from that modified local file. I have tried pulling out the LocalFilesSettingsObject and changing it's application name to my .dll like this:
Properties.Settings config = Properties.Settings.Default;
SettingsContext context = config.Context;
SettingsPropertyCollection properties = config.Properties;
SettingsProviderCollection providers = config.Providers;
SettingsProvider configFile = Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
configFile.ApplicationName = Assembly.GetExecutingAssembly().GetName().Name;
config.Initialize(context, properties, providers);
config.Reload();
这是行不通的。我奋力环绕整个.NET设置乱了我的头。我想配方来完成这个任务。我也想的设置应该如何工作.NET 2.0中一个明确的解释的链接(举例)
That is not working. I am struggling to wrap my head around the whole .NET Settings mess. I'd like a recipe to finish this task. I would also like a link to a clear explanation (with examples) of how settings are supposed to work in .NET 2.0
推荐答案
您将需要加载 x.dll.config
(与配置API)的自己。所有自动文件处理(包括 .Settings
)是所有的machine.config / y.exe.config /用户设置。
You will need to load the x.dll.config
(with the Configuration API) yourself. All the automatic file handling (including the .Settings
) is all about machine.config/y.exe.config/user-settings.
要打开一个名为配置文件:
To open a named config file:
- 参考
System.Configuration.dll
组装。 - 使用
System.Configuration
-
创建code,如:
- Reference
System.Configuration.dll
assembly. - Using
System.Configuration
Create code like:
Configuration GetDllConfiguration(Assembly targetAsm) {
var configFile = targetAsm.Location + ".config";
var map = new ExeConfigurationFileMap {
ExeConfigFilename = configFile
};
return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
这篇关于从插件模块读取dll.config(不App.config的!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!