c# - 是否可以在依赖 dll 的应用程序配置中定义 <configSections>

标签 c# configuration configsection

我有一个应用程序的自定义 .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/

相关文章:

c# - 使用相同 NuGet 包的两个不同版本

c# - 公开自托管服务时以编程方式从配置文件中读取 WCF 行为元素

visual-studio - 比较Visual Studio中的配置?

c# - 具有两个不同子集合的自定义配置部分

c# - 如何使用 C# 检索 .config 文件中的自定义配置部分列表?

c# - 使用 NetworkStream.WriteAsync 检测错误

c# - 有没有办法判断用户是否点击了屏幕上的任意位置?

C# GetAsyncKeyState 无法正常工作

java - 如何使用HttpSecurity(Spring Security)应用角色?

wcf - 如何在 WCF 客户端的配置文件中使用 configSource 的多个位置