自定義配置文件的使用

經常在使用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節點不可以重複,否則系統運行通不過。
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章