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

目录
相关文章
|
3天前
|
开发框架 搜索推荐 前端开发
【.NET全栈】ASP.NET开发Web应用——Web部件技术
【.NET全栈】ASP.NET开发Web应用——Web部件技术
|
22天前
|
人工智能 开发框架 Devops
.NET技术概览:** 本文探讨了.NET的核心特性,包括多语言支持、Common Language Runtime、丰富的类库和跨平台能力,强调其在企业级、Web、移动及游戏开发中的应用。
【7月更文挑战第4天】.NET技术概览:** 本文探讨了.NET的核心特性,包括多语言支持、Common Language Runtime、丰富的类库和跨平台能力,强调其在企业级、Web、移动及游戏开发中的应用。此外,讨论了.NET如何通过性能优化、DevOps集成、AI与ML支持以及开源策略应对未来挑战,为开发者提供强大工具,共创软件开发新篇章。
21 3
|
22天前
|
人工智能 前端开发 开发工具
**.NET技术概览:** 本文探讨.NET的核心优势
【7月更文挑战第4天】**.NET技术概览:** 本文探讨了.NET的核心优势,如统一开发平台、Visual Studio的强大工具、跨平台能力及丰富的类库。它在现代应用中的创新应用包括企业级、Web、移动、云服务和游戏开发。同时,面对性能优化、容器化、AI集成等挑战,.NET正寻求未来机遇,通过开源社区持续发展。开发者应抓住这些趋势,利用.NET推动软件创新。
26 1
|
22天前
|
人工智能 前端开发 开发工具
.NET技术探析:优势、创新应用及挑战。
【7月更文挑战第4天】**.NET技术探析:优势、创新应用及挑战。本文分三部分展开,阐述了.NET作为统一多语言开发平台的核心优势,如强大的Visual Studio工具、跨平台能力与丰富的类库;探讨了其在企业级、Web、移动及游戏开发中的创新角色;并指出面临性能优化、容器化、AI集成等挑战及未来开源社区驱动的发展机遇。通过理解与应对,开发者可借助.NET推动软件开发进步。**
22 0
|
前端开发 NoSQL .NET
一起谈.NET技术,重构TekPub——从ASP.NET MVC框架迁移到Ruby on Rails
  TekPub是一个面向开发人员的站点,致力于为开发人员提供一系列主题的在线培训,主题范围非常广泛,从微软的O/R Mapping框架Microsoft Entity Framework,到如何使用Ruby on Rails技术编写自己的日志引擎等内容都有涉及。
1611 0
|
Web App开发 SQL 前端开发
一起谈.NET技术,鲜为人知的ASP.NET MVC 2.0框架高效之谜
  要想建立开发环境,你需要安装Visual Studio 2008/2010 Beta 2,以及SQL Express 2005(可免费从MSDN下载)和MVC 2.0框架。我把本文中的示例Web应用命名为“Employee Master Information”。
999 0
|
XML 前端开发 .NET
一起谈.NET技术,ASP.NET MVC 2生成动态表单的一种最简单的思路
  在BPM、OA等系统中,都会存在一个表单设计器。有些是通过操作gridview来完成一个表单的设计;有些是通过类似VS拖拽的方法完成一个表单的设计。很明显后面一种优越于前面一种。无论是哪种,最后都会产生一些XML之类的表单结构的数据。
1295 0
|
Web App开发 缓存 .NET
一起谈.NET技术,ASP.NET缓存全解析4:应用程序数据缓存
  ASP.NET缓存全解析文章索引 ASP.NET缓存全解析1:缓存的概述 ASP.NET缓存全解析2:页面输出缓存 ASP.NET缓存全解析3:页面局部缓存 ASP.NET缓存全解析4:应用程序数据缓存 ASP.NET 缓存全解析5:文件缓存依赖 ASP.NET 缓存全解析6:数据库缓存依赖 ASP.NET 缓存全解析7:第三方分布式缓存解决方案 Memcached和Cacheman   System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类。
899 0
|
.NET
一起谈.NET技术,在ASP.NET 2.0中数据绑定的实现方法
1、为什么ASP.NET 2.0中的数据绑定控件不需要写代码就能完成更新、删除、新建等数据操作?   ASP.NET 1.x时,DataGrid等控件使用DataBinder.Eval(Container.DataItem,"ColumnName")这样的表达式可以将数据源中的数据绑定到控件上,但并不能在更新数据时自动将控件中的新值取出,更新回数据库。
1118 0
一起谈.NET技术,ASP.NET2.0服务器控件之类型转换器
类型转换器是实现自定义服务器控件属性过程中比较重要的内容。本文将对类型转换器的基本概念和实现方法进行介绍。  1. 类型转换器基本概念  类型转换器是自定义服务器控件的辅助性功能实现。它主要用于执行从字符串表示形式到指定类型之间的双向转换。
687 0