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 isCodeProgramSet to load specified resources

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

//There are many ways to get the code assembly

 

 

System. reflection. Assembly ASM = system. reflection. Assembly. getexecutingassembly ();

Stream manifestresourcestream = NULL;

Manifestresourcestream = ASM. getmanifestresourcestream ("mylibrary." + filename );

Return manifestresourcestream;

// Obtain the file stream, which can also be a ghost stream.

// If the list corresponding to our resource file is an image file

System. Drawing. Bitmap bitmap = Newsystem. Drawing. Bitmap (manifestresourcestream, true );

// If our resource file is a cursor file

Cursor cursor = new cursor (manifestresourcestream );

// Icon

Icon = new icon (manifestresourcestream );

// String

System. Io. streamreader sr = Newsystem. Io. streamreader (manifestresourcestream );

String STR = Sr. Readline ();


 

// 2.Another method is to use the GetObject () method of the ResourceManager class.

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

 

 

System. Resources. ResourceManager manager =

New system. Resources. ResourceManager ("Resource Name", assembly. getexecutingassembly ());

Object target = manager. GetObject ("resource file name"); // gets the value of the specified resource.

// The list of resource files that we add here,

// Switch back to my non-target value, which may lead to a unpacking process

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

// If our resource file is a cursor file

Cursor cursor = (cursor) target;

// Icon

Icon = (icon) target;

// String

String STR = target. tostring (); // or manager

Manager. getstring ("resource file name ");


 

//The preceding two operations allow you to load a specified resource from the resource file.

//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 that we can read resource files, we can write resource files.

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

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

 

 

system. resources. resourcewriter writer =

new system. resources. resourcewriter ("dongpad.com. resources ");

writer. addresource ("bitmap", image. fromfile (@ "dongpad. bitmap ");

writer. addresource ("cursor", image. fromfile (@ "dongpad. cursor ");

writer. addresource ("ICO", image. fromfile (@ "dongpad. ICO ");

writer. addresource ("str", image. fromfile (@ "dongpad. str ");

// here, the resource name must be unique.

writer. generate ();

writer. close ();


 

//Next, 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, or later Article 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.

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, 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 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.