程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> .NET 2.0 ConfigurationSection/Element/ElementCollection

.NET 2.0 ConfigurationSection/Element/ElementCollection

編輯:.NET實例教程
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Configuration
...{
    public class UserDefinedConfig:System.Configuration.ConfigurationSection
    ...{
        const string _author = "author";
        const string _createddate = "createddate";
        const string _connection = "connection";
        const string _collection = "collection";

        [ConfigurationProperty(_author, IsRequired = true)]
        public string Author
        ...{
            get ...{ return this[_author] as string; }
            set ...{ this[_author] = value; }
        }

        [ConfigurationProperty(_createddate)]
        public string CreatedDate
        ...{
            get ...{ return this[_createddate] as string; }
            set ...{ this[_createddate] = value; }
        }

        [ConfigurationProperty(_connection)]
        public DataBaseConnectionElement Connection
        ...{
            get ...{ return this[_connection] as DataBaseConnectionElement; }
            set ...{ this[_connection] = value; }
        }

        [ConfigurationProperty(_collection, IsRequired = false)]
        public UserDefinedConfigurationElementCollection Collection
        ...{
            get ...{ return this[_collection] as UserDefinedConfigurationElementCollection; }
        }
    }

    public class DataBaseConnectionElement : System.Configuration.ConfigurationElement
    ...{
        const string _key = "key";
        const string _tns = "tns";
        const string _userid = "userid";
        const string _password = "passWord";

        [ConfigurationProperty(_key,IsKey=true)]
        public string Key
        ...{
            get ...{ return base[_key] as string; }
            set ...{ base[_key] = value; }
        }

        [ConfigurationProperty(_tns,IsRequired=true)]
        public string TNS
        ...{
            get ...{ return base[_tns] as string; }
            set ...{ base[_tns] = value; }
        }

        [ConfigurationProperty(_userid, IsRequired = true)]
        public string UserID
        ...{
            get ...{ return base[_userid] as string; }
            set ...{ base[_userid] = value; }
        }

        [ConfigurationProperty(_passWord, IsRequired = true)]
        public string PassWord
        ...{
            get ...{ return base[_passWord] as string; }
            set ...{ base[_passWord] = value; }
        }
    }

    public class UserDefinedConfigurationElementCollection : System.Configuration.ConfigurationElementCollection
    ...{
        protected override ConfigurationElement CreateNewElement()
        ...{
            return new DataBaseConnectionElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        ...{
            return ((DataBaseConnectionElement)element).Key;
        }
    }
}

vs 配置:



            System.Configuration.Configuration conf = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(this.Request.ApplicationPath);
            conf.Sections.Add("CustomConfig", new Configuration.UserDefinedConfig());
            conf.Save();

 



<?XML version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="CustomConfig" type="Configuration.UserDefinedConfig, Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </configSections>
  <CustomConfig author="abyliver.zhang">
    <collection>
      <add key="aa1" tns="aa2" userid="aa3" passWord="aa4"/>
      <add key="bb1" tns="bb2" userid="bb3" passWord="bb4"/>
    </collection>
  </CustomConfig>
</configuration>

vs 讀取:



            System.Collections.IEnumerator em = (System.Configuration.ConfigurationManager.GetSection("CustomConfig") as Configuration.UserDefinedConfig).Collection.GetEnumerator();
            while(em.MoveNext())
            ...{
                this.lblConfiguration.Text += ((Configuration.DataBaseConnectionElement)em.Current).Key + " - ";
            }


參考:
http://www.antardev.cn/showArticle.ASP?id=44
http://www.cnblogs.com/SW515/archive/2006/08/21/482411.Html

進階:
SerializableConfigurationSection

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved