c# - 我的 dll 配置文件或为什么无法解析部分..?

标签 c# .net configuration-files

我遇到了一个问题,我无法解析文档标准配置。

例如:

private string GetConfigKey(string param)
        {
            Configuration dllConfig = ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
            AppSettingsSection dllConfigAppSettings = (AppSettingsSection)dllConfig.GetSection("appSettings");
            return dllConfigAppSettings.Settings[param].Value;
        }

因此,我从表单中的文件中获取设置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="host" value="mail.ololo.by"/>
  </appSettings>
  <configSections>
      <section name="provideConfig" type="System.Configuration.DictionarySectionHandler"/>
      <section name="provideMailStatus" type="System.Configuration.DictionarySectionHandler" />
  </configSections>
 <provideConfig>
     <add key="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23534c505763444257460d4c4f4c4f4c0d415a" rel="noreferrer noopener nofollow">[email protected]</a>" value="Mail Subject 1"/>
     <add key="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096e7c6e68496e687d6c276665666566276b70" rel="noreferrer noopener nofollow">[email protected]</a>" value="Mail Subject 2"/>
 </provideConfig>
  <provideMailStatus>
    <add key="status1" value="send"/>
    <add key="status2" value="draft"/>
    <add key="status2" value="other"/>
  </provideMailStatus>
</configuration>

但是

Hashtable hashtable =
                (Hashtable)ConfigurationManager.GetSection("provideConfig");
 foreach (DictionaryEntry dictionaryEntry in hashtable)
            {
                Console.WriteLine(dictionaryEntry.Key+" "+dictionaryEntry.Value);
            }

但不幸的是 configSections 有问题。我似乎无法理解。 MB,我走错方向了?

附注配置文件不能命名为“app.config” - 仅项目 dll 名称

最佳答案

配置文件应命名为可执行文件名和“.config”。即使这个可执行文件也使用这个 dll 文件。 例如:ConSoleApplication.exe 使用 MyLibrary.dll。然后配置文件必须命名为ConSoleApplication.exe.config

如果您需要配置文件的其他名称,请阅读 this

关于c# - 我的 dll 配置文件或为什么无法解析部分..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12705294/

相关文章:

winforms - 如何在 C# 中更新 app.config connectionstring 数据源值?

c++ - 配置文件在项目 C++ 中的位置

c# - 收集 Azure 中的 Service Fabric 群集日志

c# - 什么是基于规则的算法?

c# - 如何在 GAC 中注册 .NET DLL 文件?

.net - Azure 中的外部服务的负载平衡

c# - 使用 FieldOffset 的结构意外行为

c# - 我们如何在 C# 中创建参数化属性

c# - 你应该压缩 html 页面吗?如果是这样,如何在 .net 中做到这一点?

grails - 如何配置settings.groovy以使用多个存储库?