2012-10-13 16 views
10


Jestem całkiem początkującym z sekcjami konfiguracyjnymi w C#
Chcę utworzyć niestandardową sekcję w pliku konfiguracyjnym. Co Próbowałem po googlowania jest jak następuje
Config file:
Sekcja konfiguracji niestandardowej w 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; } 
     } 
    } 
} 


Kiedy używam tego kodu do pobrania Sekcja I pojawia się błąd informujący o błędzie konfiguracji.

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


Czego mi brakuje?
Dzięki.

Edit
Co muszę ostatecznie jest

<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

Proszę przejrzeć następujące pytania: http://stackoverflow.com/questions/3935331/how-to-implement-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

Odpowiedz

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> 

Zastosowanie:

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

Dla każdego, kto chce, aby to działało szybko. 3 rzeczy: 1. MUSISZ DODAĆ Odniesienie do System.Configuration w twoich referencjach, 2. using System.Configuration; 3. przy użyciu System.Collections.Specialized; –

+2

Możesz również pominąć grupę (customAppSettingsGroup), jeśli chcesz. – Jess

1

Spróbuj użyć:

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

Trzeba zarówno nazwę grupy i sekcję niestandardową.

+0

Dziękuję za odpowiedź, ale jakoś mi to nie pomogło. spójrz na edytowaną część pytania. –

-2

Highlight naciśnij ConfigurationSection F1, Zobaczysz, że wdrożenie w witrynie MSDN zastępuje właściwość o nazwie „Właściwości "który zwraca" ConfigurationPropertyCollection ", ponieważ twoje właściwości mają pasujący atrybut tego typu, powinieneś być w stanie wypełnić ten coll ect z twoimi właściwościami, jeśli nie owinąć ich w taki sam sposób, w jaki mają faceci MS.

Powiązane problemy