2012-10-13 44 views
10


我是一個非常初學的配置部分在c#
我想在配置文件中創建一個自定義部分。我已經在Google上搜尋後嘗試是因爲如下
配置文件:
在App.config中的自定義配置部分C#

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <sectionGroup name="MyCustomSections"> 
     <section name="CustomSection" type="CustomSectionTest.CustomSection,CustomSection"/> 
    </sectionGroup> 
    </configSections> 

    <MyCustomSections> 
    <CustomSection key="Default"/> 
    </MyCustomSections> 
</configuration> 


CustomSection.cs

namespace CustomSectionTest 
{ 
    public class CustomSection : ConfigurationSection 
    { 
     [ConfigurationProperty("key", DefaultValue="Default", IsRequired = true)] 
     [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] 
     public String Key 
     { 
      get { return this["key"].ToString(); } 
      set { this["key"] = value; } 
     } 
    } 
} 


當我使用此代碼檢索第一節得到一個錯誤說配置錯誤。

var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("CustomSection"); 


我缺少什麼?
謝謝。

編輯
我需要的最終是什麼

<CustomConfigSettings> 
    <Setting id="1"> 
     <add key="Name" value="N"/> 
     <add key="Type" value="D"/> 
    </Setting> 
    <Setting id="2"> 
     <add key="Name" value="O"/> 
     <add key="Type" value="E"/> 
    </Setting> 
    <Setting id="3"> 
     <add key="Name" value="P"/> 
     <add key="Type" value="F"/> 
    </Setting> 
</CustomConfigSettings> 
+1

請查看以下問題:http://stackoverflow.com/questions/3935331/how-to-implement-a-configurationsection-with-a-configurationelementcollection http://stackoverflow.com/questions/7983127/custom- configurationsection http://stackoverflow.com/questions/4738/using-configurationmanager-to-load-config-from-an-arbitrary-location – bytebuster

回答

30

的App.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <sectionGroup name="customAppSettingsGroup"> 
     <section name="customAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </sectionGroup> 
    </configSections> 
    <customAppSettingsGroup> 
    <customAppSettings> 
     <add key="KeyOne" value="ValueOne"/> 
     <add key="KeyTwo" value="ValueTwo"/> 
    </customAppSettings> 
    </customAppSettingsGroup> 
</configuration> 

用法:

NameValueCollection settings = 
    ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettings") 
    as System.Collections.Specialized.NameValueCollection; 

if (settings != null) 
{ 
foreach (string key in settings.AllKeys) 
{ 
    Response.Write(key + ": " + settings[key] + "<br />"); 
} 
} 
+13

對於任何人想要得到這個快速工作。 3件事情:1.您必須在您的引用中添加對System.Configuration的引用,2.使用System.Configuration; 3.使用System.Collections.Specialized; –

+2

如果需要,也可以省略組(customAppSettingsGroup)。 – Jess

1

嘗試使用:

var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("MyCustomSections/CustomSection"); 

您需要的段組的名稱和自定義欄目。

+0

感謝您的回覆,但這對我來說不起作用。請看看問題的編輯部分。 –

-2

突出的ConfigurationSection按F1鍵, 您將看到MSDN網站上的實現覆蓋一個名爲「Properties屬性「,它返回一個」ConfigurationPropertyCollection「,因爲你的屬性具有該類型的匹配屬性,你應該能夠填充這個coll如果不是以同樣的方式包裝他們的話,那麼你的財產就會與你們的財產一樣。

相關問題