一起谈.NET技术,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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值