How to use resources in dotnet

Source: Internet
Author: User
How to use resource files

--------------------------------------------------------------------------------

Summary
A very sophisticated localization system in. NET is defined in the System.Resources namespace. But most people are confused by missingmanifestresourceexception this error. This article is about what a resource file is, what it does and how to properly invoke it to avoid some "weird" bugs, including missingmanifestresourceexception.

--------------------------------------------------------------------------------

Directory
The source and ultimate purpose of this article
What is a resource file
Resource file type
Several ways to invoke a resource file
How to define the logical location of a resource file exactly
Recommended Tools
Summarize
Reference information
About the author

--------------------------------------------------------------------------------

The source and ultimate purpose of this article
Most recently, the authors have seen a lot of questions about resource files in newsgroups, and most people have missingmanifestresourceexception this very fashionable mistake. So the author thought about it and decided not to let more people waste their time on this issue. This is the reason for the birth of this article. And the ultimate goal in addition to let this problem in the newsgroup disappeared in addition to let everyone in 10 minutes thoroughly grasp the call of resources, so as to become a real resource master! :)

--------------------------------------------------------------------------------

What is a resource file
Preparing World-ready programs in. NET requires three steps, globalization,localizability, and Localization. In this third step, Localization is the most common place to use resource files. (This article does not discuss the World-ready program, perhaps later in another article) because the logical interface of the program needs to be isolated from the resource interface, and the resource interface is what we call a resource file. As the name suggests, a resource file is of course full of resources, but what is a resource? The so-called resources here are the data available in the program, such as strings, pictures, and any binary data, including any type of file. Note that a resource file can have multiple language versions, for example, a strings.resources file can have English version, Simplified Chinese version, Traditional Chinese version and so on. ResourceManager can automatically confirm which version to call based on the filename. A different version simply adds the locale language to the file name. For example, our strings.resources is the default version, the English version can be strings.en-us.resources (American English), the Simplified Chinese can be Strings.zh-chs.resources (Simplified Chinese), while traditional Chinese can be strings.zh-cht.resources (traditional Chinese). The so-called default version is that when the appropriate resource version is not found, the resources used are generally English. The default file should be embedded in the main Assembly so that no resource error can be found. Setting the property of a file to Embedded Resource in Vs.net allows the resource to be embedded in the primary Assembly.

--------------------------------------------------------------------------------

Resource file type
System.Resources namespace supports three kinds of resource files:
. txt file, can only have string resources. Because it cannot be embedded in the Assembly, it is easily exposed and modified by the customer. The biggest disadvantage is that only string resources are supported, so it is not recommended.
. resx files, composed of XML, that can be added to any resource, including binary. Also cannot be embedded in the Assembly. A class that has a dedicated read-write in the System.Resources name space. Vs.net creates such a file and then converts it into a. resources file and embeds it into the Assembly according to the settings.
The. resources file, PE format, can be added to any resource. The only file that can be embedded in a Assembly is a class that has a dedicated read-write in the System.Resources namespace.

--------------------------------------------------------------------------------

Several ways to invoke a resource file
ResourceManager can return different local resources based on different UICulture settings (this is related to the World-ready program, not discussed here), we just need to know that the calling resource uses it. Let's look at how to invoke each of the following:
. txt file:
Cannot be invoked directly, it must be converted to a. resources file before it can be used. (see "Recommended Tools" for how to convert)
. resx files:
You can use ResXResourceReader to read, but this method is not intuitive and it is not recommended to call. resx files directly. The correct approach is to convert it to a. resources file and then use ResourceManager for read work. Note If. resx files are added in Vs.net, they are automatically set to Embedded Resource, which are then embedded into the Assembly after they are converted to the. resources file.
. resources file:
Divided into two situations:
To be embedded or compiled into satellite Assembly:
Use the various constructor of ResourceManager to obtain resources in Assembly.
Individual files, not compiled or embedded in Assembly:
You can use ResourceManager.CreateFileBasedResourceManager to get the resource set (ResourceSet), which is all the resources.
Special case:
Another special case is when you embed a resource directly, that is, to embed a resource directly into the Assembly without a resource file. This can be implemented in vs.net by setting the Build property of a file to Embedded Resource. In this case the ResourceManager is useless because it can only get the. resources file (in or out of Assembly). So how do you invoke this kind of resource? It's not difficult, we need to take advantage of some of the Reflection features. Don't be afraid, not to let you learn Reflection, in fact, we just know some System.Reflection.Assembly this class some of the functions on it. There are three related functions, but we just need to assembly.getmanifestresourcestream this function. This function returns a resource embedded in Assembly as a stream, and we can turn this stream into an object that is available in. NET. For example, if the embedded resource is a picture, then we can use the New Bitmap (Stream) This Bitmap constructor to get the Bitmap object of this picture resource.
Note: Here are only a few ways to get a different resource, and see the documentation for how to use each class and function.

--------------------------------------------------------------------------------

How to define the logical location of a resource file exactly
I think this is a lot of people pay attention to a section of the most! Here the author will explain how to correctly fill in the constructor of Resoucemanager (string, Assembly), and how to complete the correct assembly.getmanifestresourcestream (string Because the two principles are the same. Having read the above description, it is much simpler to be here. The main discussion here is how to fill out that String. This String is the full name of the resource, and a full name consists of its namespace and the front part of the file name (BaseName). For example, if the default namespace (root namespace) is DefaultNamespace, and the name of the resource file is Strings.en-us.resources, then its full name is defaultnamespace.strings. This is very simple, but how to determine the name space? This is a bit odd, because C # compilers are somewhat different from vb.net compilers. Here, the author gives two compilers how to automatically add namespaces to embedded resources:
C#
It automatically adds the default namespace (same as root namespace), but also adds the name of the subfolder. For example, the resource file strings.en-us.resources in the subfolder subfolder, its full name is default namespace + subfolder + base name = Defaultnamespace.sub Folder. Strings
vb.net
It's simple in vb.net, which automatically adds root namespace to embedded resources. Regardless of which subfolder you place the resource file in, the full name of the resource file is always root namespace + base name.
According to the description above, if we use C # to add a resource file called Images.resources in the NewFolder subfolder with vs.net, then we should get those resources using the following code, assuming that default namespace is Myde Fault
ResourceManager res = new ResourceManager ("MyDefault.NewFolder.Images", this. GetType (). Assembly);
But if we use vb.net, it should be like this:
Dim Res as New ResourceManager ("Mydefault.images", Me.GetType (). Assembly)

--------------------------------------------------------------------------------

Recommended Tools
A tool in Resgen.exe:SDK that is designed to make conversions between resource file types. Supports conversion between. txt <->. resx <->. resources.
Resourcer: Designed to create resource files, easy to use, support. resx and. resources file formats. (http://www.aisto.com/roeder/dotnet)
. NET Reflector: Used to browse Assembly. If you are unsure of the full name of a resource file, you can use this tool to view it in Assembly. (http://www.aisto.com/roeder/dotnet)

--------------------------------------------------------------------------------

Summarize
The following are covered in this article:
What is a resource
What is a resource file
What types of resource files are in. NET
How to define the logical location of a resource file
Several ways to invoke a resource file
This article solves the very fashionable missingmanifestresourceexception by correctly locating the resource file. This article gives you a very rich resource to invoke experience. If you want to fully understand any possible problems with resource files, there are also vulnerabilities. If there is, I hope you understand!

--------------------------------------------------------------------------------

Reference information
Here is a link to some of the documents, if your help is in Chinese please add ". 2052" after Msdnvs:
Resource File Generator (Resgen.exe)
Ms-help://ms. Vscc/ms. Msdnvs/cptools/html/cpgrfresourcefilegeneratorutilityresgenexe.htm
Resources in Applications
Ms-help://ms. Vscc/ms. Msdnvs/cpguide/html/cpconcreatingusingresources.htm
Resource fallback Process
Ms-help://ms. Vscc/ms. Msdnvs/cpguide/html/cpconpackagingdeployingresources.htm #cpconpackagingdeployingresourcesanchor1

--------------------------------------------------------------------------------

About the author
Author: Wei Yuan (kefroth)


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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.