資源簡(jiǎn)介
應(yīng)用程序中,經(jīng)常需要保存一些用戶(hù)的設(shè)置,所以就需要把這些設(shè)置保存到一個(gè)配置文件中,以便隨時(shí)讀取和寫(xiě)入。
這個(gè)代碼就是用C#寫(xiě)的保存和讀取程序運(yùn)行目錄下的XML配置文件的程序,配置文件也會(huì)自動(dòng)生成,很靈活的。
共享給大家,大家可以很方便的調(diào)用,然后保存和讀取自己的程序設(shè)置。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
namespace?ConfigMgrTest
{
????public?partial?class?MainForm?:?Form
????{
????????public?MainForm()
????????{
????????????InitializeComponent();
????????}
????????private?void?btnSave_Click(object?sender?EventArgs?e)
????????{
????????????if?(this.txtUserID.Text.Trim().Equals(string.Empty))
????????????{
????????????????MessageBox.Show(“請(qǐng)輸入用戶(hù)名“);
????????????????this.txtUserID.Focus();
????????????????return;
????????????}
????????????if?(this.txtPassword.Text.Trim().Equals(string.Empty))
????????????{
????????????????MessageBox.Show(“請(qǐng)輸入密碼“);
????????????????this.txtPassword.Focus();
????????????????return;
????????????}
????????????//保存
????????????SystemConfig.WriteConfigData(“UserID“?this.txtUserID.Text.Trim());
????????????SystemConfig.WriteConfigData(“Password“?this.txtPassword.Text.Trim());
????????????this.txtUserID.Text?=?null;
????????????this.txtPassword.Text?=?null;
????????????MessageBox.Show(“成功保存到配置文件“?+?Application.StartupPath?+?“SystemConfig.xml?\n點(diǎn)擊讀取按鈕進(jìn)行讀取!“);
????????}
????????private?void?btnClose_Click(object?sender?EventArgs?e)
????????{
????????????//讀取
????????????this.txtUserID.Text?=?SystemConfig.GetConfigData(“UserID“?string.Empty);
????????????this.txtPassword.Text?=?SystemConfig.GetConfigData(“Password“?string.Empty);
????????}
????}
}
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件???????3291??2008-12-20?13:39??ConfigMgrTest\ConfigMgrTest.csproj
?????文件????????168??2008-12-20?13:39??ConfigMgrTest\ConfigMgrTest.csproj.user
?????文件???????1544??2008-12-20?13:43??ConfigMgrTest\MainForm.cs
?????文件???????4897??2008-12-20?13:39??ConfigMgrTest\MainForm.Designer.cs
?????文件???????5814??2008-12-20?13:39??ConfigMgrTest\MainForm.resx
?????文件????????483??2008-12-20?13:32??ConfigMgrTest\Program.cs
?????文件???????1280??2008-12-20?13:32??ConfigMgrTest\Properties\AssemblyInfo.cs
?????文件???????2853??2008-12-20?13:32??ConfigMgrTest\Properties\Resources.Designer.cs
?????文件???????5612??2008-12-20?13:32??ConfigMgrTest\Properties\Resources.resx
?????文件???????1096??2008-12-20?13:32??ConfigMgrTest\Properties\Settings.Designer.cs
?????文件????????249??2008-12-20?13:32??ConfigMgrTest\Properties\Settings.settings
?????文件??????11699??2008-12-20?13:42??ConfigMgrTest\SystemConfig.cs
?????文件????????928??2008-12-20?13:32??ConfigMgrTest.sln
????..A..H.?????22528??2008-12-20?13:43??ConfigMgrTest.suo
?????目錄??????????0??2008-12-20?13:32??ConfigMgrTest\Properties
?????目錄??????????0??2008-12-20?13:43??ConfigMgrTest
-----------?---------??----------?-----??----
????????????????62442????????????????????16
評(píng)論
共有 條評(píng)論