首页 > 代码库 > 自定义配置文件的使用

自定义配置文件的使用

经常在使用APP.CONFIG 或WEB.CONFIG

时,发现系统中的配置无法满足自己的需求。这时就需要自定义的配置文件处理:

现需要在配置文件中定义可增加删除任务的功能。可根据需要增加一个或删除任务。

定义配置节点

    /// <summary>
    /// MRP自定义配置类
    /// </summary>
    public class MRPSection : ConfigurationSection
    {
        /// <summary>
        /// Gets the tasks.
        /// </summary>
        /// <value>The tasks.</value>
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public TaskCollectionElement Tasks
        {
            get { return (TaskCollectionElement)this[""]; }
        } 
    } 

配置集合

    /// <summary>
    /// 任务集合
    /// </summary>
    public class TaskCollectionElement : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new TaskElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TaskElement)element).CompanyID;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName
        {
            get { return "Task"; }
        }

        public TaskElement this[int index]
        {
            get { return (TaskElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
    } 

定义任务类

    public class TaskElement : ConfigurationElement
    {
        [ConfigurationProperty("CompanyID", IsRequired = true)]
        public string CompanyID
        {
            get { return (string)base["CompanyID"]; }
        }

        [ConfigurationProperty("DatabaseName", IsRequired = true)]
        //[RegexStringValidator(可以在这里用正则验证配置文件中的值是否正确)]
        public string DatabaseName
        {
            get { return this["DatabaseName"] as string; }
        }

        [ConfigurationProperty("Level", IsRequired = true)]
        public string Level
        {
            get { return this["Level"] as string; }
        }

[ConfigurationProperty("AppIP", IsRequired = true)] public string AppIP { get { return this["AppIP"].ToString(); } } [ConfigurationProperty("AppName", IsRequired = true)] public string AppName { get { return this["AppName"].ToString(); } } }

配置文件如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MRPSetting"  type="MRP.MRPSection,MRP"/>
  </configSections>  
   <MRPSetting >
    <Task CompanyID="e1" DatabaseName="e1" Level="2" AppIP="192.168.1.1" AppName="host"/>
    <Task CompanyID="e2" DatabaseName="e2" Level="3" AppIP="192.168.1.1" AppName="host"/>
    <Task CompanyID="e3" DatabaseName="e3" Level="1"  AppIP="192.168.1.1" AppName="host"/>
   </MRPSetting>  
</configuration>

如何访问:

 MRPSection mrpsetting = (MRPSection)ConfigurationManager.GetSection("MRPSetting");

 foreach (TaskElement task in mrpsetting.Tasks)
            { Console.WriteLine(task.CompanyID); }

 ----------------------------------------------------------------------------------------------------------------------------------------------------------

若自定义的节点不需要集合类,则只需要如下定义即可

    public class TaskSection : ConfigurationSection
    {
        [ConfigurationProperty("CompanyID", IsRequired = true)]
        public string CompanyID
        {
            get { return (string)base["CompanyID"]; }
        }

        [ConfigurationProperty("DatabaseName", IsRequired = true)]
        //[RegexStringValidator(可以在这里用正则验证配置文件中的值是否正确)]
        public string DatabaseName
        {
            get { return this["DatabaseName"] as string; }
        }

        [ConfigurationProperty("Level", IsRequired = true)]
        public string Level
        {
            get { return this["Level"] as string; }
        }
 

        [ConfigurationProperty("AppIP", IsRequired = true)]
        public string AppIP
        { get { return this["AppIP"].ToString(); } }


        [ConfigurationProperty("AppName", IsRequired = true)]
        public string AppName
        { get { return this["AppName"].ToString(); } }
    }
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Task"  type="MRPAutoRun.TaskSection,MRPAutoRun"/>
  </configSections>
 
    <Task CompanyID="e1" DatabaseName="e1" Level="24" AppIP="192.168.1.1" AppName="host"/>     
</configuration>

此Task节点不可以重复,否则系统运行通不过。