一起谈.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 

目录
相关文章
|
4月前
|
域名解析 缓存 Linux
如何让你的.NET WebAPI程序支持HTTP3?
如何让你的.NET WebAPI程序支持HTTP3?
52 2
如何让你的.NET WebAPI程序支持HTTP3?
|
7月前
|
安全 Shell 数据库
「学习记录」.NET程序的数据库密码解密
「学习记录」.NET程序的数据库密码解密
|
8月前
|
XML 存储 JSON
使用自定义XML配置文件在.NET桌面程序中保存设置
本文将详细介绍如何在.NET桌面程序中使用自定义的XML配置文件来保存和读取设置。除了XML之外,我们还将探讨其他常见的配置文件格式,如JSON、INI和YAML,以及它们的优缺点和相关的NuGet类库。最后,我们将重点介绍我们为何选择XML作为配置文件格式,并展示一个实用的示例。
98 0
|
3天前
|
XML 开发框架 .NET
LabVIEW中加载.NET 2.0,3.0和3.5程序集
LabVIEW中加载.NET 2.0,3.0和3.5程序集
12 4
|
3天前
|
开发框架 .NET 开发工具
LabVIEW加载.NET程序集
LabVIEW加载.NET程序集
|
4天前
|
XML 开发框架 .NET
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
|
6月前
|
开发框架 .NET 测试技术
.NET Core 日志记录程序和常用日志记录框架
本文主要内容为.NET Core的日志记录程序和常使用的日志记录框架的简单使用 首先,打开VS2019新建一个ASP.NET Core Web Api项目,项目创建好后会有一个集成好的天气预报的类和控制器,接下来,我们的方法就在天气控制器里完成。
51 0
|
8月前
|
开发框架 .NET C#
如何判断一个 Dot Net 程序是 32 位还是 64 位?
如何判断一个 Dot Net 程序是 32 位还是 64 位?
|
4月前
|
开发框架 前端开发 小程序
分享46个ASP.NET博客程序源码,总有一款适合您
分享46个ASP.NET博客程序源码,总有一款适合您
34 0
|
5月前
|
存储 开发框架 .NET
Asp.Net第一章入门之后台处理程序
Asp.Net第一章入门之后台处理程序
31 0