2014-03-26 59 views
0

我有一个阅读从应用程序配置自定义部分的问题。 我app.config定制部分阅读

的app.config:

<configuration> 
    <configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > 
     <section name="StartupFolders" type="Test.FolderConfiguration, Test"/> 
    </sectionGroup> 
    </configSections> 

    <applicationSettings> 
    <StartupFolders> 
     <Folders> 
     <Folder folderType="A" path="c:\foo" /> 
     <Folder folderType="B" path="C:\foo1" /> 
     </Folders> 
    </StartupFolders> 
    </applicationSettings> 
</configuration> 

FolderConfiguration.cs:

namespace Test 
{ 
    public class FolderElement : ConfigurationElement 
    { 

     [ConfigurationProperty("folderType", DefaultValue = "", IsKey = true, IsRequired = true)] 
     public string FolderType 
     { 
      get { return ((string)(base["folderType"])); } 
      set { base["folderType"] = value; } 
     } 

     [ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)] 
     public string Path 
     { 
      get { return ((string)(base["path"])); } 
      set { base["path"] = value; } 
     } 
    } 

    [ConfigurationCollection(typeof(FolderElement))] 
    public class FoldersCollection : ConfigurationElementCollection 
    { 
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new FolderElement(); 
     } 

     protected override object GetElementKey(ConfigurationElement element) 
     { 
      return ((FolderElement)(element)).FolderType; 
     } 

     public FolderElement this[int idx] 
     { 
      get { return (FolderElement)BaseGet(idx); } 
     } 
    } 
    class FolderConfiguration : ConfigurationSection 
    { 
     [ConfigurationProperty("Folders")] 
     public FoldersCollection FolderItems 
     { 
      get { return ((FoldersCollection)(base["Folders"])); } 
     } 
    } 
} 
在MainWindow.cs

最后,我用它都像:

Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
      FolderConfiguration section = (FolderConfiguration)cfg.Sections["StartupFolders"]; 

返回我部分== null。我可以;不明白我做错了什么。任何帮助,请

回答

0
class FolderConfiguration : ConfigurationSection 
{ 
    [ConfigurationProperty("Folders")] 
    [ConfigurationCollection(typeof(FoldersCollection), AddItemName = "Folder"] 
    public FoldersCollection Folders 
    { 
     get { return ((FoldersCollection)(base["Folders"])); } 
    } 
} 

还,您FolderElements属性,您有:

get { return ((string)(base["folderType"])); } 
set { base["folderType"] = value; } 

它应该是:

get { return ((string)(this["folderType"])); } 
set { this["folderType"] = value; } 

这是否帮助任何?

编辑

我立足我的答案关闭this

这是否返回什么? var folderConfig = ConfigurationManager.GetSection(“StartupFolders”)作为FolderConfiguration; if(folderConfig!= null){//通过集合循环}

+0

不,它不。再次获得空格 – amplifier

+0

没有例外或非凡的输出。程序正常工作。只需folderConfig == null。乍一看,我和上面的页面几乎一样 – amplifier

0

请看this question。它只有一个级别(在引用的问题是用户),但我没有看到StartupFolders-> Folders-> Folder的原因。您可以只有

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
     <section name="Folders" type="ConsoleApplication1.FoldersConfigMapSection, ConsoleApplication1"/> 
    </configSections> 
    <Folders> 
     <Folder folderType="A" path="c:\foo" /> 
     <Folder folderType="B" path="C:\foo1" /> 
    </Folders> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
</configuration>