How to use resource files
Summary
There is a very complete set of localization systems in .NET is defined in the System.Resources namespace. However, most people are confused by MissingmanifestResourceException. This article is to let everyone know what is resource file, what is used and how to call to avoid some "strange" errors, including MissingManifestResourceException This common error.
table of Contents
Source and ultimate goal of this article
What is a resource file?
Resource file type
Several methods of calling resource files
How to accurately define the logical location of the resource file
Recommended tool
to sum up
Reference Information
About author
Source and ultimate goal of this article
Recent authors see many questions about resource files in the newsgroup, and most people have MissingmanifestResourceException this very fashionable error. So the author considers it, decided not to let more people have wasted time on this issue. This is the cause of this article. In addition to let this issue disappear on the newsgroup, let everyone completely master the call of resources within 10 minutes, thus becoming a real resource master! :)
What is a resource file?
It takes three steps, Globalization, LocalizAbility and Localization, which are prepared in .NET. In this third step, the localization is the most common place to use the resource file. (This article does not discuss the World-Ready program, maybe in another article) because the logical interface of the program needs to isolate the resource interface, and the resource interface is what we said. As the name suggests, it is of course all resources in a resource file, but what is the resources? The so-called resources herein are 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, Simplified Chinese, Traditional Chinese version. ResourceManager can automatically confirm which version is called according to the file name. Different versions can only be added to the area language in the file name. For example, our strings.resources is the default version, English version can be strings.en-us.resources (US English), Simplified Chinese can be strings.zh-chs.resources (Simplified Chinese), and traditional Chinese Can be strings.en-cht.resources (Traditional Chinese). The so-called default version is to use resources when you can't find the appropriate resource version, usually in English. The default file should be embedded in the primary assembly so that the error cannot occur. Set the properties of a file to Embedded Resource to embed the resource in the primary Assembly in VS.NET.
Resource file type
System.Resources name space supports three resource files:
.txt file, only string resources. Since it is not possible to be embedded in Assembly, it is easy to expose and is modified by customers. The biggest disadvantage is that only string resources are supported, so it is not recommended.
.resx file, consisting of XML, any resource can be added, including binary. It is also not possible to be embedded in Assembly. There is a dedicated read and write class in the System.Resources name space. VS.NET Creates this file and then converts it to the .resources file and 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 the assembly, which has a dedicated read and written class in the System.Resources namespace. Several methods of calling resource files
ResourceManager can return different local resources (this is related to the World-Ready program based on different UICULTURE settings), and we only need to know that the resource is used to use it. Let us take a look at how to call each:
.txt file:
You can't call them directly, you can first convert it into .resources files. (See "Recommended Tools" on how to translate
.resx file:
You can use the ResxResourceReader to do read, but this method is not intuitive, not recommended to call directly .resx file. The correct way is to convert it into a .resources file, then use the ResourceManager to read work. Note If the .resx file added in the VS.NET, then they are automatically set to Embedded Resource, which is embedded in Assembly after turning into .resources files.
.resources file:
Divided into two cases:
Embedded or compiled into Satellite Assembly:
Use resourceManager's various constructors to get resources in Assembly.
Separate files, 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, it is not directly embedded in Assembly without passing a resource file. This can be implemented in the vs.net of the build attribute of setting a file as an Embedded Resource. In this case, ResourceManager is useless because it can only get the .resources resource file (or not in assembly). So how do you call this type of resource? It is not difficult, we need to take advantage of some of the features in Reflection. Don't be afraid, don't let you learn the reflection, in fact, we just know some of the functions in the class of SYSTEM.REFLECTION.ASSEMBLY. There are three related functions, but we only need this function of Assembly.getManifestResourceStream. This function returns a resource that is embedded in Assembly in a stream, and we can convert this stream into the object available in .NET. For example, if the embedded resource is a picture, then we can use the new bitmap (stream) this Bitmap's CONSTRUctor to get the Bitmap object of this image resource.
Note: Only how to obtain different resources here, please refer to the relevant documentation on how to use each class and function.
How to accurately define the logical location of the resource file
I think this is the most concerned about many people! Here, the author will explain how to correctly fill in ResouceManager (String, Assembly), and how to fill in Assembly.getManifestResourceStream (String) correctly because they are the same. I have seen the above description, it is more simple here. What is mainly discussed here how to fill in the String. This string is the full name of the resource, and a full name consists of its namespace and filename (BaseName). For example, if the default name space (root namespace) is defaultnamespace, 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 somewhat surprises, because the C # compiler is somewhat different from the compiler of VB.NET. The author gives two compilers here to automatically add namespaces to embedded resources: C #
It automatically adds DEFAULT NAMESPACE (same as root namespace), but also adds the name of the subfolder. For example, resource files put on the Subfolder subfolder Strings.en-US.Resources, its full name is Default Namespace Subfolder Base Name = defaultnamespace.subfolder.strings
VB.NET
It is very simple in VB.NET, which automatically adds root namespace to embedded resources. No matter which subfolder is placed in which the resource file is placed, the full name of the resource file is always root namespace base name.
According to the above description, if we use the C #, use vs.net to add a resource file called Images.Resources in the NewFolder subfolder, then we should use the following code to get these resources, assuming that default namespace is MyDefault:
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 (). Assmbly)
Recommended tool
ResGen.exe: Tools in SDK, specifically used to transform between resource file types. Support. TXT <-> .resx <-> .resources between.
Resourceer: Designed to create a resource file, easy to use, support .resx and .resources file format. (Http://www.aisto.com/roeder/dotnet)
.NET Reflector: Used to browse Assembly. If you are not sure the full name of a resource file, you can use this tool to view in Assembly. (Http://www.aisto.com/roeder/dotnet)
to sum up
This article talks about the following:
What is resource
What is a resource file?
What kind of resource files in .NET?
How to define the logical location of the resource file
Several methods of calling resource files
This article solves the very fashionable MissingmanifestResourceException by properly positioning the resource file. This article gives you a very rich resource call experience. I want you to fully understand any possible problems of the resource file, of course, there will be loopholes. If you have, I hope everyone will understand!
Reference Information
The following is a link to some documents. If your help is 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 author
Author: Yuan Wei (Kefroth)
Email: kefroth@Interlap.com.ar