【问题标题】:app.config custom section readingapp.config 自定义部分阅读
【发布时间】:2014-03-26 08:05:33
【问题描述】:

我在从应用配置中读取自定义部分时遇到问题。 我有

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>

文件夹配置.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"];

它返回我 section == null。我无法理解我做错了什么。请帮忙

【问题讨论】:

    标签: .net app-config


    【解决方案1】:
    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") as FolderConfiguration; if( folderConfig != null) { //循环遍历集合 }

    【讨论】:

    • 没有异常或异常输出。程序正常运行。只是 folderConfig == null。乍一看,我和上面的页面差不多
    【解决方案2】:

    请看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>
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 2015-08-11
      • 2014-09-28
      • 2011-01-30
      • 2011-10-02
      • 2015-07-05
      • 2011-05-25
      • 2010-09-23
      • 1970-01-01
      相关资源
      最近更新 更多