我有一个应用程序的自定义 .NET 插件,我正在尝试为插件的配置文件创建 configSections。问题是如果使用 OpenMapperExeConfiguration/OpenExeConfiguration 加载配置,我无法阅读该部分。
这是我的配置文件(MyTest.dll.config)
<configuration>
<configSections>
<section name="test" type="MyTest, Test.ConfigRead"/>
</configSections>
<test>
..Stuff here
</test>
<appSettings>
<add key="uri" value="www.cnn.com"/>
</appSettings>
</configuration>
这是我访问测试配置部分的代码示例
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + "config";
Configuration applicationConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
//Using OpenExeConfiguration doesnt help either.
//Configuration applicationConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
//Accessing test section
applicationConfig.GetSection("test");
//Accessing AppSettings works fine.
AppSettingsSection appSettings = (AppSettingsSection)applicationConfig.GetSection("appSettings");
appSettings.Settings["uri"].Value;
如图所示,可以很好地读取 appsettings 值。除了主应用程序的配置文件之外,是否可以在任何其他配置中使用 configSections?
最佳答案
配置设置适用于应用程序(app.config 位于应用程序的根目录中用于 .EXE,Web 应用程序的 Web 根目录)和机器(machine.config 位于 [System Root]\Microsoft.NET\Framework[CLR Version]\配置)级别。
唯一使用的其他配置文件是用于创建程序集版本控制策略并通过使用 AL 工具链接到程序集的策略配置文件。这显然是您不想做的。
尝试将加载项的配置部分合并到当前应用程序的配置部分以创建一个应用程序级别的配置文件,或者将它们放在 machine.config 文件中。
关于c# - 是否可以在依赖 dll 的应用程序配置中定义 <configSections>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/446570/