這是asp.net程式來讀取多語言版本Ini設定檔的開發樣本,主要分為以下三個部分:
1、 Ini 設定檔
2、 讀取Ini設定檔的DLL
3、 Web頁面調用與內容顯示
4、 資料庫表T_User,如右圖
首先說明一下Ini 檔案格式:如其中[M_Index]節點和該節點下的所有的key和value,
其中[M_Index]節點的名稱是對應開發樣本中的每個頁面所在的檔案夾名稱的第一個字母加底線再加該頁面的名稱組合而成,如 M_Index 則表示Manager檔案夾下面有一個Index.aspx 頁面,這樣就避免了不同檔案夾裡面有相同頁面而導致頁面內容顯示的問題,其中的key對應頁面變數value對應頁面顯示的內容。
的解決方案中DLL檔案夾中ConfigureManager.dll 就是讀取Ini 設定檔的一個封裝類,提供方法來擷取某個節點裡面指定key的value。
建立項目
準備工作好了以後,下面就開始建立一個項目,開啟VS 建立一個項目並命名為“LanVersionSwitch”。
1. 建立一個檔案夾DLL 添加現有項把ConfigureManager.dll 添加進來,並添加引用該dll
2. 建立檔案夾INI添加現有項把ConfigCn.ini和ConfigEn.ini 加進來
3. 在web.config 中添加配置資訊:
Web.config
<appSettings>
<add key="filePathEn" value="INI/ConfigEn.ini"/>
<add key="filePathCn" value="INI/ConfigCn.ini"/>
</appSettings>
4. 建立檔案夾Common 並添加一個類LanSwitch.cs來調用dll方法進行再次封裝以供web頁面調用:
LanSwitch
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ConfigureManager;
using System.Web.Caching;
namespace LanVersionSwitch.Common
{
public class LanSwitch
{
private static readonly object obj = new object();
public static string GetValue(string section, string key, string lan)
{
string filePath;
if(HttpContext.Current.Cache[section + "_" + key + "_" + lan] == null)
{
lock (obj)
{
if (HttpContext.Current.Cache[section + "_" + key + "_" + lan] == null)
{
if (lan.ToUpper() == "EN")
{
filePath =Environment.CurrentDirectory + "/" + System.Configuration.ConfigurationManager.AppSettings["filePathEn"].ToString();
}
else
{
filePath = Environment.CurrentDirectory + "/" + System.Configuration.ConfigurationManager.AppSettings["filePathCn"].ToString();
}
ManagerConfigIni mi = new ManagerConfigIni(filePath);
HttpContext.Current.Cache.Add(section + "_" + key + "_" + lan, mi.GetIniKeyValueForStr(section, key), null, DateTime.Now.AddSeconds(5), TimeSpan.Zero, CacheItemPriority.Normal, null);
}
}
}
return HttpContext.Current.Cache[section + "_" + key + "_" + lan].ToString();
}
}
}
5. 在Common檔案夾增加DataAccess.cs 用來訪問資料庫,判斷登入使用者名稱和密碼以及修改語言版本。
6. 建立頁面Login.aspx如:
7. 建立檔案夾Manager 並添加web頁面Index.aspx 如
8. 在Manager 檔案夾裡面建立PersonalSet.aspx 如:
9. 在Login.aspx 頁面登入按鈕進行登入判斷,代碼
Title
protected void Button1_Click(object sender, EventArgs e)
{
DataAccess da = new DataAccess();
DataSet ds=da.Login(this.TextBox1.Text, this.TextBox2.Text);
if (ds.Tables[0].Rows.Count>0)
{
Session["lan"] = ds.Tables[0].Rows[0]["lan"];
Session["username"] = ds.Tables[0].Rows[0]["username"];
Response.Redirect("Manager/Index.aspx");
}
else
{
this.Label3.Text = "登入失敗";
}
}
10. Index.cs 的代碼:
Title
private string path_page = "M_Index";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetValue();
}
}
private void SetValue()
{
string lan = Session["lan"].ToString();
this.Button3.Text = LanSwitch.GetValue(path_page, "menu1", lan);
this.Button4.Text = LanSwitch.GetValue(path_page, "menu2", lan);
this.Button5.Text = LanSwitch.GetValue(path_page, "menu3", lan);
this.Button6.Text = LanSwitch.GetValue(path_page, "menu4", lan);
this.Button7.Text = LanSwitch.GetValue(path_page, "menu5", lan);
this.Button8.Text = LanSwitch.GetValue(path_page, "menu6", lan);
}
11. PersonalSet.cs 的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LanVersionSwitch.Common;
namespace LanVersionSwitch.Manager
{
public partial class PersonalSet : System.Web.UI.Page
{
private string path_page = "M_PersonalSet";
DataAccess da = new DataAccess();
protected void Button1_Click(object sender, EventArgs e)
{
if (this.RadioButton1.Checked)
{
Session["lan"] = "CN";
}
else
{
Session["lan"] = "EN";
}
da.UpdateLan(Session["lan"].ToString(),Session["username"].ToString());
SetValue();
}
private void SetValue()
{
string lan = Session["lan"].ToString();
this.RadioButton1.Text = LanSwitch.GetValue(path_page,"radio1" , lan);
this.RadioButton2.Text = LanSwitch.GetValue(path_page, "radio2", lan);
this.Button1.Text = LanSwitch.GetValue(path_page, "save" , lan);
this.Button2.Text = LanSwitch.GetValue(path_page, "return", lan);
if (lan == "EN")
{
this.RadioButton1.Checked = false;
this.RadioButton2.Checked = true;
}
else
{
this.RadioButton1.Checked = true;
this.RadioButton2.Checked = false;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetValue();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Index.aspx");
}
}
}
12. 從以上的代碼圖可以看到 SetValue() 主要是頁面調用LanSwitch.cs 的方法GetValue(string pagename,string key,string lan)來進行頁面內容顯示,
其中每個頁面的pagename 都是有當前頁面所在檔案夾第一個字母加”_”再加當前頁面的名稱組成。
總結:到這裡,已經可以產生並運行代碼看下運行結果,一個簡單的多語言版本切換程式就寫好了。
讀取Ini 設定檔的一個封裝類,提供方法來擷取某個節點裡面指定key的value,以下是DLL :
/Files/xiaogelove/ConfigureManager.rar