建立 工程, 用Enterprise Library Configuration開啟 web.config
建立 configsection .
重新命名為 Edit ,
然後在edit 結點建立
XML File Storage Provider
Xml Serializer Transformer
設定 XML File Storage Provider 它的filename 為存取資訊的 config檔案
(你要先在項目中建立一個config 檔案,在試例中命名為 Setting.config)
然後儲存
using System;
using System.Text;
using System.Xml.Serialization;
namespace Intlib
{
/**//// <summary>
/// Persen 的摘要說明。
/// </summary>
public class Persen
{
private string name ;
private int age;
private string country;
public Persen()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
public string Name
{
get{ return name; }
set{ name = value; }
}
public int Age
{
get{return age ;}
set{age=value ;}
}
public string Country
{
get{return country ;}
set{country=value ;}
}
}
}
一個為寫入,一個為讀取,
private void Button1_Click(object sender, System.EventArgs e)
{
Persen ps=new Persen ();
ps.Name=this.TextBox1 .Text ;
ps.Age =int.Parse (this.TextBox2.Text) ;
ps.Country =this.TextBox3.Text ;
ConfigurationManager.WriteConfiguration ("Edit",ps);
}
private void Button2_Click(object sender, System.EventArgs e)
{
Persen ps = ConfigurationManager.GetConfiguration("Edit") as Persen ;
this.TextBox1 .Text =ps.Name;
this.TextBox2.Text =ps.Age.ToString ();
this.TextBox3.Text =ps.Country ;
}
企業庫會對對象自動進行序列化,和還原序列化...
方便從 config中讀取和寫入.....