asp.net程序來讀取多語言版本Ini配置文件

作者: 小格  來源: 博客園  發布時間: 2010-11-21 21:41  閱讀: 2193 次  推薦: 0   原文鏈接   [收藏]  
摘要:這是asp.net程序來讀取多語言版本Ini配置文件的開發示例。

  這是asp.net程序來讀取多語言版本Ini配置文件的開發示例,主要分為以下三個部分:

  1、 Ini 配置文件

  2、 讀取Ini配置文件的DLL

  3、 Web頁面調用與內容顯示

  4、 數據庫表T_User,如右圖 

  首先說明一下Ini 文件格式:如下圖其中[M_Index]節點和該節點下的所有的keyvalue,其中[M_Index]節點的名稱是對應開發示例中的每個頁面所在的文件夾名稱的第一個字母加下劃線再加該頁面的名稱組合而成,如 M_Index 則表示Manager文件夾下面有一個Index.aspx 頁面,這樣就避免了不同文件夾里面有相同頁面而導致頁面內容顯示的問題,其中的key對應頁面變量value對應頁面顯示的內容。

  上圖的解決方案中DLL文件夾中ConfigureManager.dll 就是讀取Ini 配置文件的一個封裝類,提供方法來獲取某個節點里面指定keyvalue

  新建項目 

  準備工作好了以后,下面就開始新建一個項目,打開VS 新建一個項目并命名為“LanVersionSwitch”。

  1. 新建一個文件夾DLL 添加現有項把ConfigureManager.dll 添加進來,并添加引用該dll

  2. 新建文件夾INI添加現有項把ConfigCn.iniConfigEn.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頁面調用:

 
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();
}
}
}
LanSwitch

  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 的代碼:             

 
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);

}
Title

  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 配置文件的一個封裝類,提供方法來獲取某個節點里面指定keyvalue,以下是DLL 下載地址:

/Files/xiaogelove/ConfigureManager.rar 

0
0
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()