一起谈.NET技术,asp.net程序来读取多语言版本Ini配置文件

简介:   这是asp.net程序来读取多语言版本Ini配置文件的开发示例,主要分为以下三个部分:  1、 Ini 配置文件  2、 读取Ini配置文件的DLL  3、 Web页面调用与内容显示  4、 数据库表T_User,如右图    首先说明一下Ini 文件格式:如下图其中[M_Index]...

  这是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 

目录
相关文章
|
8月前
|
开发框架 安全 .NET
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
.NET Framework是Windows平台的开发框架,包含CLR和FCL,支持多种语言开发桌面、Web应用。常用版本有3.5、4.5.2、4.8.1,系统可同时安装多个版本,确保软件兼容运行。
1939 0
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
|
数据挖掘 BI
.net8 Syncfusion生成pdf/doc/xls/ppt最新版本
通过使用 Syncfusion,您可以高效地生成各种文档,满足不同的业务需求。这些工具不仅易于使用,还具有高性能和高度可扩展性,是处理文档的理想选择。
700 16
|
开发框架 前端开发 .NET
VB.NET中如何利用ASP.NET进行Web开发
在VB.NET中利用ASP.NET进行Web开发是一个常见的做法,特别是在需要构建动态、交互式Web应用程序时。ASP.NET是一个由微软开发的开源Web应用程序框架,它允许开发者使用多种编程语言(包括VB.NET)来创建Web应用程序。
467 6
|
开发框架 JSON .NET
ASP.NET Core 标识(Identity)框架系列(三):在 ASP.NET Core Web API 项目中使用标识(Identity)框架进行身份验证
ASP.NET Core 标识(Identity)框架系列(三):在 ASP.NET Core Web API 项目中使用标识(Identity)框架进行身份验证
406 1
|
开发框架 搜索推荐 前端开发
【.NET全栈】ASP.NET开发Web应用——Web部件技术
【.NET全栈】ASP.NET开发Web应用——Web部件技术
|
开发框架 .NET 开发工具
【Azure 应用服务】App Service 的.NET Version选择为.NET6,是否可以同时支持运行ASP.NET V4.8的应用呢?
【Azure 应用服务】App Service 的.NET Version选择为.NET6,是否可以同时支持运行ASP.NET V4.8的应用呢?
205 0
|
JSON API C#
闲话 .NET(6):.NET Core 各个版本的特性
闲话 .NET(6):.NET Core 各个版本的特性
631 0
|
开发框架 .NET 数据库连接
ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?
ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?
432 0
|
开发框架 NoSQL .NET
使用 Asp.net core webapi 集成配置系统,提高程序的灵活和可维护性
使用 Asp.net core webapi 集成配置系统,提高程序的灵活和可维护性
335 0