C#自定义配置未读取

时间:2017-10-30 12:47:11

标签: c# .net

我正在用C#开发一个程序,这是我第一次使用自定义配置。

我的程序正常启动,但配置文件似乎无法读取。

这是我的“app.config”配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="Files" type="Initialisation.FilesSection, Initialisation" >
      <section name="Source" type="Initialisation.SourceElement, Initialisation" />

      <section name="List" type="Initialisation.ListElementCollection, Initialisation" />
      <section name="File" type="Initialisation.FileElement, Initialisation" />
    </sectionGroup>
  </configSections>

  <Files>
    <Source path="\\192.168.21.6\tmp\" />

    <List>
      <File name="Adobe Reader"       file="readerdc_fr_xa_crd_install.exe"           default="true"  />
      <File name="Adobe Flash"        file="flashplayer27pp_xa_install.exe"           default="false" />
      <File name="Java"               file="java.exe"                                 default="true"  />
    </List>
  </Files>
</configuration>

这是管理文件的类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Initialisation
{
    public class Files
    {
        //private static FilesSection _config;

        private Files()
        {
        }

        public static FilesSection Config
        {
            get
            {
                /*if (_config == null)
                {
                    _config = global::System.Configuration.ConfigurationManager.GetSection("Files") as FilesSection;
                }
                return _config;*/
                return ((FilesSection)(ConfigurationManager.GetSection("Files")));
            }
        }
    }

    public class FilesSection : System.Configuration.ConfigurationSection
    {

        [System.Configuration.ConfigurationPropertyAttribute("Source")]
        public SourceElement Source
        {
            get
            {
                return ((SourceElement)(this["Source"]));
            }
        }

        [System.Configuration.ConfigurationPropertyAttribute("List")]
        [System.Configuration.ConfigurationCollectionAttribute(typeof(FileElement), AddItemName = "File")]
        public ListElementCollection List
        {
            get
            {
                return ((ListElementCollection)(this["List"]));
            }
        }
    }

    public class SourceElement : System.Configuration.ConfigurationElement
    {

        [System.Configuration.ConfigurationPropertyAttribute("path", IsRequired = true)]
        public string Path
        {
            get
            {
                return ((string)(this["path"]));
            }
            set
            {
                this["path"] = value;
            }
        }
    }

    public class ListElementCollection : System.Configuration.ConfigurationElementCollection
    {

        public FileElement this[int i]
        {
            get
            {
                return ((FileElement)(this.BaseGet(i)));
            }
        }

        protected override System.Configuration.ConfigurationElement CreateNewElement()
        {
            return new FileElement();
        }

        protected override object GetElementKey(System.Configuration.ConfigurationElement element)
        {
            return ((FileElement)(element)).Name;
        }
    }

    public class FileElement : System.Configuration.ConfigurationElement
    {

        [System.Configuration.ConfigurationPropertyAttribute("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return ((string)(this["name"]));
            }
            set
            {
                this["name"] = value;
            }
        }

        [System.Configuration.ConfigurationPropertyAttribute("file", IsRequired = true)]
        public string File
        {
            get
            {
                return ((string)(this["file"]));
            }
            set
            {
                this["file"] = value;
            }
        }

        [System.Configuration.ConfigurationPropertyAttribute("default", IsRequired = false, DefaultValue = false)]
        public bool Default
        {
            get
            {
                return ((bool)(this["default"]));
            }
            set
            {
                this["default"] = value;
            }
        }
    }
}

我想获得这样的价值:

FilesSection fs = Files.Config;
SourceElement se = fs.Source;
string path = se.Path;

MessageBox.Show(path);

但所有变量(fs,se和path)都为空。

我阅读了很多帖子,我已经尝试了很多解决方案:/

1 个答案:

答案 0 :(得分:0)

问题是 Files 类是 ConfigurationSection ,但您在app.config上将其声明为 sectionGroup 。其他元素也是如此,它们是 ConfigurationElement ,但被声明为 section 元素。

configSections 中删除所有 ConfigurationElements ,因为它们不需要声明,并将文件声明从 sectionGroup 更改为部分

您的 configSections 应如下所示:

<configSections>
    <section name="Files" type="Initialisation.FilesSection, Initialisation" />
</configSections>