This is a development example for the Asp.net program to read the ini configuration file of a multilingual version. It consists of the following three parts:
1. ini configuration file
2. Read the DLL of the ini configuration file
3. Web page call and content display
4. Database Table t_user, as shown in the right figure
First, describe the INI file format, for example, the [m_index] node and all the keys and values under the node,
The name of the [m_index] node is a combination of the first letter of the folder name corresponding to each page in the development example and the name of the page, for example, m_index indicates that there is an index under the manager folder. in this way, the page content is displayed because the same page exists in different folders. The key corresponds to the page variable value corresponding to the page content.
In the DLL folder, configuremanager. dll is an encapsulation class that reads the ini configuration file. It provides methods to obtain the value of the specified key in a node.
Create a project
After the preparation is complete, create a project, open vs, create a project, and name it "lanversionswitch ".
1. Create a folder DLL and add existing items to add configuremanager. dll, and add reference to this DLL.
2. Create a folder INI and add existing items to add configcn. ini and configen. ini.
3. Add configuration information in Web. config:
Web. config
<Deleetask>
<Add key = "filepattern" value = "INI/configen. ini"/>
<Add key = "filepathcn" value = "INI/configcn. ini"/>
</Appsettings>
4. Create a folder common and add a class lanswitch. CS to call the DLL method and encapsulate it again for the web page to call:
Lanswitch
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. deleettings ["filepattern"]. tostring ();
}
Else
{
Filepath = environment. currentdirectory + "/" + system. configuration. configurationmanager. deleettings ["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 ();
}
}
}
5. Add dataaccess. CS in the common folder to access the database, determine the logon username and password, and modify the language version.
6. Create a login. ASPX page, for example:
7. Create a Folder Manager and add index. aspx on the web page, as shown in figure
8. Create personalset. aspx in the Manager folder, for example:
9. log on to the login. ASPX page and check the logon code.
Title
Protected void button#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 = "Logon Failed ";
}
}
10. Index. CS code:
Title
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 );
}
11. Code for 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 button#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. From the code diagram above, we can see that setvalue () is mainly used to display the page content by calling the lanswitch. Cs method getvalue (string pagename, string key, string LAN) on the page,
The pagename of each page is composed of the first letter and "_" in the folder where the current page is located, and the name of the current page.
Conclusion: here, You can generate and run the code to view the running results. A simple multi-language version switch program is ready.
An encapsulation class that reads the ini configuration file and provides methods to obtain the value of the specified key in a node. below is the DLL:
/Files/xiaogelove/configuremanager.rar