Http://blog.9cbs.net/etmonitor/.net system learning --- Globalization & Resources
l
l Understand resource files
l Create a resource file
l Use the resource file in the program
l Naming and deployment of resource files
l
Preface:
Before learning how to use .NET resource files and how to develop World-Ready programs, let's take a look at why you want to use resource files and use it.
Suppose you want to set the FORM TITLE and LOGO based on the current CULUTRE:
Private Void Form1_Load (Object Sender, System.Eventargs E) {
CultureInfo Ci = New CultureInfo (Thread.currentthread.currentuicultuicultuicultuicultuicultuicultuicultuicultuiculture.toString ());
Switch (ci.tostring (). TOLOWER ()) {
Case "en-cn": // Chinese version
THIS.TEXT = formtitle_zh_cn;
Imglogo.image = new bitmap (Application.Startuppath "/Logo_zh_cn.jpg");
Break;
Case "EN-US": // English version
THIS.TEXT = formtitle_EN_US;
Imglogo.image = new bitmap (Application.Startuppath "/Logo_en_us.jpg");
Break;
Default: // Default version
THIS.TEXT = formtitle_neutral;
Imglogo.image = new bitmap (Application.Startuppath "/Logo_neutral.jpg");
Break;
}
}
This code has two questions:
First, the logo file is exposed to the user, and it is stored in the format of ordinary files, which causing other programs or users to modify these files; users who save hard disk space may also choose to delete it, these may result Application error. Make sure the picture or any other file and the unique security mode together is embedded as a resource file in the program set and load.
Second, this is a World-Ready program. If you need to join a new Culture, you may have to change your source code, join new Case, then recompilation to adapt to new Culture needs, this is a world- The READY program is unrealistic. Developing a World-Ready program is important to ensure the logical interface of the program and the isolation of the resource interface. Add a new Culture resource at any time, we should not recoilate the source. Instead, we only need to prepare the new resource files, then publish it to the user and deploy it in the appropriate directory. Applications should be able to find appropriate resources based on different Culture.
The purpose of this article is to help readers understand what is Resources and how to use resources to eliminate the two issues mentioned above.
The full text is divided into four parts:
The first part is some and resource-related concepts.
The second part is an example program (RESOURCEGENERATOR) to explain how to create a resource file.
The third part is another example program (WorldApp), used to explain how to use the resource file in the program is a naming and deployment of resource files. Introduce the naming method of the .NET and how to configure the resource file in the World-Ready program.
First part concept
First understand some concepts:
1. What is a resource file?
As the name suggests, the resource document is of course all resources. However, what is the resource? The so-called resource here is any data available in the program, such as a string, a picture, or any binary format. A resource file can have multiple language culture versions, for example, a culture.resources file can have English version, Simplified Chinese version of Japanese version. ResourceManager can automatically confirm which version of the call is confirmed according to the Culture and resource file names. Only different resource versions need to join language culture information in the file name (.resource file has a strict naming specification, refer to Section IV: Naming and deployment of resource files).
2. Type of resource files
System.Resources namespace support three types of resources:
.txt file, only string resources. Because it is not possible to be embedded in Assembly, it is easy to expose, and it is modified by other programs or users. The biggest disadvantage is that only string resources are supported, not recommended.
.resx file, consisting of XML, any resource can be added, including binary format. It is also not possible to be embedded in Assembly. There is a dedicated read and write class in the System.Resources namespace. This file created in VS.NET is also converting it to the .resources file and then embed it in Assembly based on the settings.
.resources file, PE format, you can join any resource. The only file that can be embedded in Assembly, and has a dedicated read and write class in the System.Resources namespace.
3. Several methods of calling resource files
ResourceManager can return different local resources according to different UICULTURE settings. Different Culture's resource files have a strict naming rule, only by naming according to this rule, CRL can find this local resource according to Culture. PS: Because this is very important, just J. Reference Part IV: Naming and Deployment of Resource Files)
.txt file:
You can't call them directly, you can first convert it into .resources files.
.resx file:
You can use the ResxResourceReader to read, but this method is not intuitive and is not safe, and it is not recommended to call directly .resx file. The correct way is to convert it into a .resources file and then read with ResourceManager. Note that if it is added to the .resx file added in the VS.NET, then they are automatically set to Embedded Resource, and then be converted to Assembly after being converted to .resources files.
.resources file:
Divided into two cases:
· Satellite assembly (Satellite Assembly) is embedded or compiled (Satellite assembly):
Use resourceManager's various constructors to get resources in Assembly.
· Separate files, are not compiled or embedded in Assembly:
You can use the ResourceManager.createFileBaseDResourceManager to get the resource set (resourceset), which is all resources. Special case:
There is also a special case, that is, when you embed a resource, that is, you don't pass a resource (.resources) to embed a resource (Object) directly in Assembly. This can be embedded in Assembly through the parameter / Embed: