一起谈.NET技术,asp.net程序来读取多语言版本Ini配置文件
2011-09-02 00:20
886 查看
这是asp.net程序来读取多语言版本Ini配置文件的开发示例,主要分为以下三个部分:
1、 Ini 配置文件
2、 读取Ini配置文件的DLL
3、 Web页面调用与内容显示
4、 数据库表T_User,如右图
![](http://images.cnblogs.com/cnblogs_com/xiaogelove/table.JPG)
首先说明一下Ini 文件格式:如下图其中[M_Index]节点和该节点下的所有的key和value,其中[M_Index]节点的名称是对应开发示例中的每个页面所在的文件夹名称的第一个字母加下划线再加该页面的名称组合而成,如 M_Index 则表示Manager文件夹下面有一个Index.aspx 页面,这样就避免了不同文件夹里面有相同页面而导致页面内容显示的问题,其中的key对应页面变量value对应页面显示的内容。
![](http://images.cnblogs.com/cnblogs_com/xiaogelove/configcn.JPG)
![](http://images.cnblogs.com/cnblogs_com/xiaogelove/configen.JPG)
![](http://images.cnblogs.com/cnblogs_com/xiaogelove/dll.JPG)
上图的解决方案中DLL文件夹中ConfigureManager.dll 就是读取Ini 配置文件的一个封装类,提供方法来获取某个节点里面指定key的value。
新建项目
准备工作好了以后,下面就开始新建一个项目,打开VS 新建一个项目并命名为“LanVersionSwitch”。
1. 新建一个文件夹DLL 添加现有项把ConfigureManager.dll 添加进来,并添加引用该dll
2. 新建文件夹INI添加现有项把ConfigCn.ini和ConfigEn.ini 加进来
3.在web.config 中添加配置信息:
Web.config
4.新建文件夹Common 并添加一个类LanSwitch.cs来调用dll方法进行再次封装以供web页面调用:
LanSwitch
5.在Common文件夹增加DataAccess.cs 用来访问数据库,判断登录用户名和密码以及修改语言版本。
6.新建页面Login.aspx如下图:
![](http://images.cnblogs.com/cnblogs_com/xiaogelove/login.JPG)
7.新建文件夹Manager 并添加web页面Index.aspx 如下图
![](http://images.cnblogs.com/cnblogs_com/xiaogelove/index.JPG)
8.在Manager 文件夹里面新建PersonalSet.aspx 如下图:
![](http://images.cnblogs.com/cnblogs_com/xiaogelove/personal.JPG)
9.在Login.aspx 页面登录按钮进行登录判断,代码
Title
10.Index.cs 的代码:
Title
11.PersonalSet.cs 的代码:
12.从以上的代码图可以看到 SetValue() 主要是页面调用LanSwitch.cs 的方法GetValue(string pagename,string key,string lan)来进行页面内容显示,
其中每个页面的pagename 都是有当前页面所在文件夹第一个字母加”_”再加当前页面的名称组成。
总结:到这里,已经可以生成并运行代码看下运行结果,一个简单的多语言版本切换程序就写好了。
读取Ini 配置文件的一个封装类,提供方法来获取某个节点里面指定key的value,以下是DLL 下载地址:
/Files/xiaogelove/ConfigureManager.rar
1、 Ini 配置文件
2、 读取Ini配置文件的DLL
3、 Web页面调用与内容显示
4、 数据库表T_User,如右图
首先说明一下Ini 文件格式:如下图其中[M_Index]节点和该节点下的所有的key和value,其中[M_Index]节点的名称是对应开发示例中的每个页面所在的文件夹名称的第一个字母加下划线再加该页面的名称组合而成,如 M_Index 则表示Manager文件夹下面有一个Index.aspx 页面,这样就避免了不同文件夹里面有相同页面而导致页面内容显示的问题,其中的key对应页面变量value对应页面显示的内容。
上图的解决方案中DLL文件夹中ConfigureManager.dll 就是读取Ini 配置文件的一个封装类,提供方法来获取某个节点里面指定key的value。
新建项目
准备工作好了以后,下面就开始新建一个项目,打开VS 新建一个项目并命名为“LanVersionSwitch”。
1. 新建一个文件夹DLL 添加现有项把ConfigureManager.dll 添加进来,并添加引用该dll
2. 新建文件夹INI添加现有项把ConfigCn.ini和ConfigEn.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 配置文件的一个封装类,提供方法来获取某个节点里面指定key的value,以下是DLL 下载地址:
/Files/xiaogelove/ConfigureManager.rar
相关文章推荐
- 一起谈.NET技术,探索Asp.net mvc 的文件上传(由浅入深)
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- 使用java程序读取配置文件中的相关属性值-asp.net关注
- ASP.NET 2.0 读取配置文件[INI]
- (转)ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- 一起谈.NET技术,ASP.NET配置错误页面浅析
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题
- [导入]ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- 一起谈.NET技术,ASP.NET调用.sql文件(二)
- 一起谈.NET技术,ASP.NET MVC 通过 FileResult 向浏览器发送文件
- 一起谈.NET技术,ASP.NET调用.sql文件
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- ASP.NET 2.0 读取配置文件[INI](示例代码下载)
- 一起谈.NET技术,解析ASP.NET应用程序中上传文件的方案