2013-05-13 86 views
4

我需要從app/web.config中的自定義節中讀取鍵值。在C#中讀取自定義配置節的鍵值

我通過

Reading a key from the Web.Config using ConfigurationManager

How can I retrieve list of custom configuration sections in the .config file using C#?

然而,他們並不指定如何讀取自定義欄目時,我們需要明確指定路徑到配置文件(在我的情況下,配置文件不在它的默認位置)

我的web.config文件的示例:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <MyCustomTag> 
    <add key="key1" value="value1" /> 
    <add key="key2" value="value2" /> 
    </MyCustomTag> 
<system.web> 
    <compilation related data /> 
</system.web> 
</configuration> 

其中我需要讀取MyCustomTag中的鍵值對。

當我嘗試(configFilePath是通向我的配置文件): -

var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath }; 

var config = 
      ConfigurationManager.OpenMappedExeConfiguration(
      configFileMap, ConfigurationUserLevel.None); 

     ConfigurationSection section = config.GetSection(sectionName); 

     return section[keyName].Value; 

我得到一個錯誤,說明在部分[的keyName]

+1

請加入你的web.config(全部或部分)。 – 2013-05-13 14:35:01

+0

與web.config中的自定義設置的路徑無關。你將使用'' – Saravanan 2013-05-13 14:38:00

+0

@FrancescoDeLisi Question edited。 – Karan 2013-05-14 05:01:49

回答

7
「無法訪問受保護的內部索引‘這個’這裏」

不幸的是,這聽起來並不容易。解決該問題的方法是使用ConfigurationManager獲取文件配置文件,然後使用原始xml。所以,我通常使用以下方法:

private NameValueCollection GetNameValueCollectionSection(string section, string filePath) 
{ 
     string file = filePath; 
     System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); 
     NameValueCollection nameValueColl = new NameValueCollection(); 

     System.Configuration.ExeConfigurationFileMap map = new ExeConfigurationFileMap(); 
     map.ExeConfigFilename = file; 
     Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
     string xml = config.GetSection(section).SectionInformation.GetRawXml(); 
     xDoc.LoadXml(xml); 

     System.Xml.XmlNode xList = xDoc.ChildNodes[0]; 
     foreach (System.Xml.XmlNode xNodo in xList) 
     { 
      nameValueColl.Add(xNodo.Attributes[0].Value, xNodo.Attributes[1].Value); 

     } 

     return nameValueColl; 
} 

,並且該方法的調用:

var bla = GetNameValueCollectionSection("MyCustomTag", @".\XMLFile1.xml"); 


for (int i = 0; i < bla.Count; i++) 
{ 
    Console.WriteLine(bla[i] + " = " + bla.Keys[i]); 
} 

結果:

enter image description here

+1

這個答案沒有提到爲什麼這種方法比定義一個自定義的section類更好(它對於簡單的情況來說更直接,但仍然)。 – 2013-05-14 05:40:24

+0

@凱謝謝。我已經接受你的答案。 @「。\ XMLFile1.xml」是配置文件的路徑,對嗎? – Karan 2013-05-14 05:45:22

+0

@Kai It works!另外,我用字典替換了NameValueCollection以獲得更好的性能。 – Karan 2013-05-14 06:58:37

0

我已經搜查了很多尋找解決方案爲同樣的問題。經過一些搜索和調試後,我在我的應用程序中使用了它。我希望它也能幫助你。

https://synvistech.wordpress.com/2014/02/12/how-to-implement-custom-configsection-in-your-configuration/

+1

請注意,[只有鏈接的答案](http://meta.stackoverflow.com/tags/link-only-answers/info)不鼓勵,所以答案應該是搜索解決方案的終點(vs.而另一個引用的中途停留時間往往會隨着時間推移而過時)。請考慮在此添加獨立的摘要,並將鏈接保留爲參考。 – kleopatra 2014-02-12 10:39:51

+0

鏈接現在消失了。這裏有一個網頁存檔鏈接:https://web.archive.org/web/20150423202332/http://synvistech.com/blogs/how-to-implement-custom-configsection-in-your-configuration – derFunk 2015-12-15 15:53:37

+0

鏈接已更新。 – 2016-02-03 09:39:26

相關問題