//
using
?System;
using ?System.Data;
using ?System.Configuration;
using ?System.Linq;
using ?System.Web;
using ?System.Web.Security;
using ?System.Web.UI;
using ?System.Web.UI.HtmlControls;
using ?System.Web.UI.WebControls;
using ?System.Web.UI.WebControls.WebParts;
using ?System.Xml.Linq;
/// ? <summary>
/// ?Summary?description?for?MyElement
/// ? </summary>
public ? class ?MyElement?:?ConfigurationElement
{
???? public ?MyElement()
????{
???????? //
???????? // ?TODO:?Add?constructor?logic?here
???????? //
????}
????[ConfigurationProperty( " userName " ,IsRequired = true )]
???? public ? string ?UserName
????{
???????? get ?{? return ?( string ) this [ " userName " ];?}
???????? set ?{? this [ " userName " ]? = ?value;?}
????}
????[ConfigurationProperty( " userPwd " )]
???? public ? string ?UserPwd
????{
???????? get ?{? return ?( string ) this [ " userPwd " ];?}
???????? set ?{? this [ " userPwd " ]? = ?value;?}
????}
}
public ? class ?MyElementCollection?:?ConfigurationElementCollection
{
????
???? public ?MyElementCollection()
????{
????????MyElement?element? = ?CreateNewElement()? as ?MyElement;
????}
???? protected ? override ?ConfigurationElement?CreateNewElement()
????{
???????? return ? new ?MyElement();
????}
???? protected ? override ? object ?GetElementKey(ConfigurationElement?element)
????{
???????? return ?((MyElement)element).UserName;
????}
???? protected ? override ? void ?BaseAdd(ConfigurationElement?element)
????{
???????? base .BaseAdd(element, false );
????}
???? public ? void ?Add(MyElement?element)
????{
????????BaseAdd(element);
????}
}
public ? class ?MyConfigSection?:?ConfigurationSection
{
????
????????MyElement?element;
???????? public ?MyConfigSection()
????????{
????????????element? = ? new ?MyElement();
????????}
????????[ConfigurationProperty( " files " )]
???????? public ?MyElementCollection?FileTypes
????????{
???????????? get
????????????{
????????????????MyElementCollection?types? = ?(MyElementCollection) base [ " files " ];
???????????????? return ?types;
????????????}
????????}
???????? protected ? override ? void ?DeserializeSection(System.Xml.XmlReader?reader)
????????{
???????????? base .DeserializeSection(reader);
????????}
???????? protected ? override ? string ?SerializeSection(ConfigurationElement?parentElement,? string ?name,?ConfigurationSaveMode?saveMode)
????????{
???????????? return ? base .SerializeSection(parentElement,?name,?saveMode);
????????}
}
using ?System.Data;
using ?System.Configuration;
using ?System.Linq;
using ?System.Web;
using ?System.Web.Security;
using ?System.Web.UI;
using ?System.Web.UI.HtmlControls;
using ?System.Web.UI.WebControls;
using ?System.Web.UI.WebControls.WebParts;
using ?System.Xml.Linq;
/// ? <summary>
/// ?Summary?description?for?MyElement
/// ? </summary>
public ? class ?MyElement?:?ConfigurationElement
{
???? public ?MyElement()
????{
???????? //
???????? // ?TODO:?Add?constructor?logic?here
???????? //
????}
????[ConfigurationProperty( " userName " ,IsRequired = true )]
???? public ? string ?UserName
????{
???????? get ?{? return ?( string ) this [ " userName " ];?}
???????? set ?{? this [ " userName " ]? = ?value;?}
????}
????[ConfigurationProperty( " userPwd " )]
???? public ? string ?UserPwd
????{
???????? get ?{? return ?( string ) this [ " userPwd " ];?}
???????? set ?{? this [ " userPwd " ]? = ?value;?}
????}
}
public ? class ?MyElementCollection?:?ConfigurationElementCollection
{
????
???? public ?MyElementCollection()
????{
????????MyElement?element? = ?CreateNewElement()? as ?MyElement;
????}
???? protected ? override ?ConfigurationElement?CreateNewElement()
????{
???????? return ? new ?MyElement();
????}
???? protected ? override ? object ?GetElementKey(ConfigurationElement?element)
????{
???????? return ?((MyElement)element).UserName;
????}
???? protected ? override ? void ?BaseAdd(ConfigurationElement?element)
????{
???????? base .BaseAdd(element, false );
????}
???? public ? void ?Add(MyElement?element)
????{
????????BaseAdd(element);
????}
}
public ? class ?MyConfigSection?:?ConfigurationSection
{
????
????????MyElement?element;
???????? public ?MyConfigSection()
????????{
????????????element? = ? new ?MyElement();
????????}
????????[ConfigurationProperty( " files " )]
???????? public ?MyElementCollection?FileTypes
????????{
???????????? get
????????????{
????????????????MyElementCollection?types? = ?(MyElementCollection) base [ " files " ];
???????????????? return ?types;
????????????}
????????}
???????? protected ? override ? void ?DeserializeSection(System.Xml.XmlReader?reader)
????????{
???????????? base .DeserializeSection(reader);
????????}
???????? protected ? override ? string ?SerializeSection(ConfigurationElement?parentElement,? string ?name,?ConfigurationSaveMode?saveMode)
????????{
???????????? return ? base .SerializeSection(parentElement,?name,?saveMode);
????????}
}
//Web.config
????
<
section?
name
="MyConfigS"
?type
="MyConfigSection"
/>
</ configSections >
</ configSections >
//Test
????
protected
?
void
?Button1_Click(
object
?sender,?EventArgs?e)
????{
????????Configuration?config? = ?WebConfigurationManager.OpenWebConfiguration( " ~ " );
????????MyConfigSection?files? = ?config.Sections[ " MyConfigS " ]? as ?MyConfigSection;
????????MyElement?obj? = ? new ?MyElement();
????????obj.UserName? = ? " sdf " ;
????????obj.UserPwd? = ? " sdf " ;
????????files.FileTypes.Add(obj);
????????config.Save();
????}
????{
????????Configuration?config? = ?WebConfigurationManager.OpenWebConfiguration( " ~ " );
????????MyConfigSection?files? = ?config.Sections[ " MyConfigS " ]? as ?MyConfigSection;
????????MyElement?obj? = ? new ?MyElement();
????????obj.UserName? = ? " sdf " ;
????????obj.UserPwd? = ? " sdf " ;
????????files.FileTypes.Add(obj);
????????config.Save();
????}
? http://www.cnblogs.com/suimengsiqu/archive/2008/10/31/provider.html
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
