How to use resource files

Source: Internet
Author: User
Tags dotnet
Abstract. Net has a complete set of localized systems defined in the system. Resources namespace. However, most people are confused by the missingmanifestresourceexception error. This article aims to let everyone know what a resource file is, how useful it is, and how to call it correctly to avoid some "strange" errors, including the common error missingmanifestresourceexception. Directory the source of this article and the ultimate goal of what is the resource file type call several methods of resource files how to accurately define the logical location of resource files recommended tools summary reference information about the author The source and ultimate goal of this article recently I have seen many questions about resource files in newsgroups, and most people have missingmanifestresourceexception, a 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 article Article . The ultimate goal is not only to let this problem disappear in the news group, but also to let everyone thoroughly grasp the resource calls within 10 minutes, so as to become a real resource master! :) What is a resource file? Prepare world-ready in. net. Program Three steps are required: globalization, Localizability, and localization. The localization in the third step is the most common place to use resource files. (This article does not discuss the world-ready program, maybe in another article later) because the logic interface of the program needs to be isolated from the resource interface, and the resource interface is what we call the resource file. As the name suggests, a resource file is of course all resources, but what is a resource? Resources are the data available in the program, such as strings, images, and any binary data, including any types of files. Note that a resource file can be in multiple languages. For example, a strings. Resources file can be in English, simplified Chinese, or traditional Chinese. ResourceManager can automatically identify the called version based on the file name. For different versions, you only need to add the regional 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), which can be strings in simplified Chinese. zh-CHS.resources (Simplified Chinese), and traditional Chinese can be strings. zh-CHT.resources (Traditional Chinese ). The default version is the resource used when the appropriate resource version cannot be found. It is generally in English. The default file should be embedded in the main assembly, so that the resource cannot be found. Setting the attribute of a file to embedded resource in vs. Net can embed the resource into the main assembly. The resource file type system. Resources namespace supports three resource files:. txt files, which can only contain string resources. Because it cannot be embedded into assembly, it is easy to expose and modified by the customer. The biggest drawback is that only string resources are supported, so it is not recommended. The. resx file is composed of XML and can be added to any resource, including binary. Cannot be embedded into assembly. There is a dedicated read/write class in the system. Resources namespace. Vs. Net creates such a file, converts it to A. Resources file, and embeds it into Assembly according to the settings. . Resources file, in PE format, can be added to any resource. The only file that can be embedded into the Assembly has a dedicated read/write class in the system. Resources namespace. Several Methods for calling resource files ResourceManager can return different local resources based on different uiculture settings (this is related to the world-ready program, which is not discussed here ), we only need to know how to call the resource to use it. Next let's take a look at how to call each type:. txt file: it cannot be called directly. You must convert it into a. Resources file before using it. (For how to convert the file, see "Recommended Tools"). resx file: You can use resxresourcereader to read the file. However, this method is not intuitive and it is not recommended to directly call the. resx file. The correct method is to convert it into a. Resources file and then use ResourceManager for reading. Note that if the. resx files are added in vs. net, they are automatically set to embedded resource, converted to. Resources files, and embedded into assembly .. Resources files: Are embedded or compiled into satellite assembly. Resources in the Assembly are obtained using various constructors of ResourceManager. A separate file is not compiled or embedded into the Assembly. You can use ResourceManager. createfilebasedresourcemanager to obtain the resource set (resourceset), which is all resources. Special case: Another special case is that when you embed a resource directly, that is, a resource is directly embedded into the assembly without a resource file. In vs. net, you can set the build attribute of a file to embedded resource. In this case, ResourceManager is useless because it can only obtain the. Resources resource file (in or not in assembly ). So how to call such resources? It's not hard. We need to take advantage of some features in reflection. Don't be afraid, it's not for you to learn reflection again. In fact, we only need to know some functions in the system. reflection. Assembly class. There are three related functions, but we only need the Assembly. getmanifestresourcestream function. This function returns a stream of resources embedded in the Assembly, and we can convert this stream into objects available in. net. For example, if the embedded resource is an image, we can use the bitmap constructor of the bitmap new Bitmap (Stream) to obtain the bitmap object of the image resource. Note: This document only describes how to obtain different resources. For more information about how to use classes and functions, see related documents. How to accurately define the logical location of resource files I think this is what many people are most concerned about! Here, I will explain how to correctly fill in the constructor resoucemanager (string, assembly) and how to correctly fill in assembly. getmanifestresourcestream (string), because they both work in the same way. After reading the above description, it is much simpler here. Here we mainly discuss how to fill in the string. This string is the complete name of the resource. A complete name consists of its namespace and the basename before the file name. For example, if the default namespace (root namespace) is defaultnamespace and the name of the resource file is strings. en-US.resources, its complete name is defaultnamespace. Strings. This is simple, but how to determine the namespace? This is a bit strange, because the C # compiler is somewhat different from the VB. NET compiler. Here, the author gives two compilers how to automatically add namespaces to embedded resources: C # It automatically adds the default namespace (the same as the root namespace), but also adds the name of the subfolders. For example, the resource file strings in the subfolder sub-folder. en-US.resources, its complete name is default namespace + subfolder + base name = defaultnamespace. subfolder. stringsvb. net in VB. net, it automatically adds the root namespace to the embedded resources. No matter which sub-folder you place the resource file, the full name of the resource file will always be root namespace + base name. According to the above description, if we use C # And use vs. Net to add a resource file named images. Resources to the newfolder sub-folder, we should use the following Code Obtain these resources. Assume that the 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 (). assembly) Recommendation tool resgen.exe: a tool in the SDK that is used to convert resource file types. Conversion Between. txt <->. resx <->. Resources is supported. Resourcer: used to create resource files. It is easy to use and supports. resx and. Resources file formats. (Http://www.aisto.com/roeder/dotnet). Net reflector: Used to browse assembly. If you are not sure about the full name of a resource file, you can use this tool to view it in assembly. Http://www.aisto.com/roeder/dotnet) This article summarizes the following content: What is a resource and what is a resource file.. net. How does one define the logical location of a resource file and call several methods of the resource file? This article solves the very fashionable missingmanifestresourceexception by correctly locating the resource file. This article provides you with a wealth of resource calling experience. If you want to fully understand any possible problems related to resource files, there will also be vulnerabilities. If you have any, please forgive me! The following are links to some documents. If your help is in Chinese, add ". 2052 ": resource file generator (resgen.exe) ms-help: // Ms. VSCC/MS. msdnvs/cptools/html/cpgrfresourcefilegeneratorutilityresgenexe.htm resources in applicationsms-help: // Ms. VSCC/MS. msdnvs/cpguide/html/cpconcreatingusingresources.htm resource fallback processms-help: // Ms. VSCC/MS. msdnvs/cpguide/html/cpconpackagingdeployingresources.htm # cpconpackagingdeployingresourcesanchor1 About the author: Yuan Wei (kefroth) Email: kefroth@interlap.com.ar

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.