簡體   English   中英

我的dll配置文件,或者為什么無法解析Section ..?

[英]My config file for dll or why cannot parse Section..?

我遇到了無法解析文檔標准配置的問題。

例如:

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名稱

配置文件應命名為可執行文件名和“ .config”。 即使此可執行文件也使用此dll文件。 例如:ConSoleApplication.exe使用MyLibrary.dll。 然后,配置文件必須命名為ConSoleApplication.exe.config

如果您需要其他名稱作為配置文件,請閱讀文件

謝謝大家,我解決了這個問題。 我可以告訴別人是否會感興趣。

配置部分設計器Codeplex

.NET配置代碼生成器

最好的問候,尤恩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM