Summary of C # resource file operations

Source: Internet
Author: User

// Here, I will summarize the operations related to resource files.

 

// 1. It is common to obtain the file stream corresponding to the resource file, and then convert it to the corresponding file

// A typical practice is to load the specified resource through the code assembly.

// The following uses the static method GetExecutingAssembly () of Assembly to obtain the Assembly

// There are many other ways to get the code assembly

C #Code

11. // if the list corresponding to our resource file is an image file

13. System. Drawing. Bitmap bitmap =NewSystem. Drawing. Bitmap (manifestResourceStream,True);

15. // if our resource file is a cursor file

17. Cursor cursor =NewCursor (manifestResourceStream );

19. // icon

21. Icon =NewIcon (manifestResourceStream );

23. // string

25. System. IO. StreamReader sr =NewSystem. IO. StreamReader (manifestResourceStream );

27.StringStr = sr. ReadLine ();

 

// 2. Another method, mainly through the GetObject () method of the ResourceManager class

// You can also get the stream through GetStream () and then perform the operation.

C #Code

11. // if the list corresponding to our resource file is an image file System. Drawing. Bitmap bitmap = (System. Drawing. Bitmap) target;

13. // if our resource file is a cursor file

15. Cursor cursor = (Cursor) target;

17. // icon

19. Icon icon = (Icon) target;

21. // string

23.StringStr = target. ToString (); // or manager

25. manager. GetString ("resource file name ");

 

// You can load a specified resource from the resource file by using the preceding two operations.

// The premise is that your resource list has this resource.

// The first method is recommended. This method is also used to operate resource files in MS Resx.

// Now we can read the resource file and start writing the resource file.

 

// 3. Of course, you can directly add a resource file through the design interface,

// Remember to change the generation operation to "embedded resources", Embed

C #Code

11. writer. AddResource ("str", Image. FromFile (@ "dongpad. str "));

13. // here, the resource name must be unique.

15. writer. Generate ();

17. writer. Close ();

 

// Next time I will summarize some of the frequently used rewriting methods in C # development. Hope everyone can support it!

-- References --------------------------------------------------------------------------

 What is a resource file? 
In. NET, three steps are required to prepare the World-Ready program: 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 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 language versions. 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.
  
  Resource file type 
The System. Resources namespace supports three resource files:
. Txt file, 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 that resources can be called to use it. Next let's take a look at how to call each of the following methods:
. Txt file:
It cannot be called directly. It must be converted to a. resources file before it can be used. (For details about how to convert the data, see "Recommended Tools ")
. Resx file:
You can use ResXResourceReader to read data. 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 file:
There are two scenarios:
Embedded or compiled into Satellite Assembly:
Use various constructor of ResourceManager to obtain resources in Assembly.
The file is not compiled or embedded into the Assembly:
You can use ResourceManager. CreateFileBasedResourceManager to obtain the resource set (ResourceSet), that is, all resources.
Special cases:
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, we will have more simple orders 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 sub-folder. For example, the full name of the resource file Strings. en-US.resources in the Subfolder subfolders is default namespace + subfolder + base name = DefaultNamespace. Subfolder. Strings
VB. NET
In VB. NET, the root namespace is automatically added to 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 #, use. NET added an image file named Images in the NewFolder sub-folder. resources resource file, we should use the following code to obtain these resources, assuming that the default namespace is MyDefault:
ResourceManager res = new ResourceManager ("MyDefault. NewFolder. Images", this. GetType (). Assembly );

 

Related Article

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.