The website page (Aspx. CS) reads the value of the key value corresponding to the resource file (*. resx)

Source: Internet
Author: User

In. net, you can use the resource source file to implement the multi-language version of the software system. Today, a resource file is added and then dynamically called on the webpage,
I have never touched on this knowledge before, so I am not sure how to add a resource file to achieve dynamic calling! Then google search found that most of them were in the following mode:
1. system. Resources. ResourceManager manager = new system. Resources. ResourceManager ("resource file name", assembly. getexecutingassembly ());
After debugging for a long time, I found that the variable "resource file name" is only applicable to resource files with the suffix "*. resources,
2. The properties. Resource method is used for loading. Later, it is found that there is no properties class in the CS class under the page (this property method can be used in a Windows application), and it does not work.

Later I found a method to read the resx resource file through vs2008 msdn. I sorted out the code and pasted it, hoping to help you:

Using system. Resources;
Using system. collections;

/// <Summary>
/// Obtain the value of the corresponding resource file based on the key value
/// </Summary>
/// <Param name = "resxfilename"> resource file path </param>
/// <Param name = "skey"> key name </param>
/// <Returns> string </returns>
Public static string getresxvalue (string resxfilename, string skey)
String valueresult = "";

If (system. Io. file. exists (resxfilename) // For example: String filepaht = server. mappath ("app_localresources/default. aspx. resx ")
Resxresourcereader AA = new resxresourcereader (resxfilename );
Foreach (dictionaryentry D in aa)
If (D. Key. tostring () = skey)
Valueresult = D. value. tostring ();

Return valueresult;


Supplement 1:
However, today we found two simpler methods to read resource files (included in)

Getglobalresourceobject (string param1, string param2): Method for reading global resource files
Parameter description: param1 (resource file class name: resource file name, without resx)
Param2 (key value name)

Getlocalresourceobject (string parma1): Method for reading local resource files
Parameter description: param1 (key value name)

SupplementTip 2:
Read the contents of the global resource file, but an error will be reported if the file does not exist after it is released.
Using system. Resources;
String filename = server. mappath (@ "app_globalresources \ logresource. resx ");
Resxresourcereader reader = new resxresourcereader (filename );
Idictionaryenumerator enumerator = reader. getenumerator ();
While (enumerator. movenext ())
String key = (string) enumerator. Key;
Object obj2 = enumerator. value;
Response. Write ("key value:" + key );
Response. Write ("content value:" + obj2 + "<br> ");

Supplement 3:
After the global resource file app_globalresources is published, it becomes app_globalresources.dll, and its resource content collection solution is read.
System. reflection. Assembly ass = system. reflection. Assembly. Load ("app_globalresources ");
Type type = ass. GetType ("resources." + "logresource ");
System. reflection. propertyinfo [] Pa = type. getproperties ();
String STRC = "";
Foreach (system. reflection. propertyinfo tmpp in PA)
// Key name
STRC + = tmpp. Name. Trim ();

// Key value
If (tmpp. getvalue (null, null )! = NULL)
STRC + = tmpp. getvalue (null, null). tostring ();

Response. Write (STRC );

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.