2

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

例如:

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="post@gate.ololo.by" value="Mail Subject 1"/>
     <add key="guga@gate.ololo.by" 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,我走错了方向?

PS Config 文件无法命名"app.config"- 只有项目 dll 名称

4

2 回答 2

2

谢谢大家,我解决了这个问题。我可以判断是否有人会感兴趣。

配置部分设计器 Codeplex

.NET 配置代码生成器

最好的问候,尤恩。

于 2012-11-02T08:35:42.237 回答
2

Config file should be named as executable file name and ".config". Even this executable file uses this dll-file. For example: ConSoleApplication.exe uses MyLibrary.dll. Then config file must be named ConSoleApplication.exe.config

If You need some other name for config file read this

于 2012-10-03T23:02:26.767 回答