About resource files

Source: Internet
Author: User

About resource files
In real life, some exe or dll files must depend on other files for normal operation. If you copy the file directly to the client, then, it is not necessarily a good solution for the program to use relative paths to read (because once the customer deletes these resources, the program may have unforeseen results ). Therefore, these files must be embedded in exe or dll and integrated with the release program. At this time, "resource files" are often our first choice.

In. NET (C # Or VB. NET), we can use two large schemes to create resource files:

1) directly create (copy a common file to the project and set its BuildAction to "Embeded Resource". At this time, when compiling the entire project, this file is considered as a special "resource file" (essentially, the compiler parses it into binary code, because all files can be read and written as binary code, the CPU in the computer only recognizes binary code) and exe are mixed and stored in special areas.

To read such resource files, use Assembly. GetManifestResourceStream. Note that this method has two versions of overload functions:

I) The first overload function only needs a string (The Namespace of the current project. [Folder name 1 ....... Folder name N]. Embedded file name. Extension ).

Ii) The second overload function requires a class type of the current executable program (accept this parameter, similar to obtaining the project Namespace of the method in method I; the string behind it is. [Folder name 1. ...... Folder name N]. File Name. Extension ).

[Example]

Assume that my project is as follows (desert.jpg has already been set as Embeded Resource, and the Namespace of the project has the same name as the project itself, which is WinFormCSharp). Now I want to read the project and display it as the Form1 image background.



 

Train of Thought Analysis:
Because desert.jpg is in the Resources folder, write as follows:
[C #]
Assembly asm = Assembly. GetExecutingAssembly (); // obtain the currently running Assembly
This. BackgroundImage = Image. FromStream (asm. GetManifestResourceStream ("WinFormCSharp.Resources.Desert.jpg "));
// You can also:
This. BackgroundImage = Image. FromStream (asm. GetManifestResourceStream (typeof (Form1), "Resources.Desert.jpg "));

[VB. NET]
Dim asm As Assembly = Assembly. GetExecutingAssembly ()
'Get the currently running Assembly
Me. BackgroundImage = Image. FromStream (asm. GetManifestResourceStream ("WinFormCSharp.Resources.Desert.jpg ")
'You can also:
Me. BackgroundImage = Image. FromStream (asm. GetManifestResourceStream (GetType (Form1), "Resources.Desert.jpg "))

Note 1: to ensure the accuracy of the Namespace (considering that some project names do not match the actual Namespace), the method for obtaining the currently running project Namespace is:
[C #]
String s = asm. GetName (). Name;
[VB. NET]
Dim s As String = asm. GetName (). Name
Then use s to splice with the sample program.

NOTE 2: You can also use GetManifestResourceNames () to check resources in all embedded projects and further determine which resources (or resources) need to be selected. The returned result is a string array:
[C #]
Foreach (var item in asm. GetManifestResourceNames ())
{
MessageBox. Show (item. ToString ());
}

[VB. NET]
For Each item In asm. GetManifestResourceNames ()
MessageBox. Show (item. ToString ())
Next

2) use the Resource. resx file. By default, such a resource file is generated simultaneously with the project under the Properties folder. Of course, you can right-click the project and add a new project... in the pop-up window ......" Add a resource in the dialog box -- note! At this time, the resource is only added to the resource file, not to the exe or dll itself, and will only enter the exe or dll after compilation.
If we are careful, we will also find that this method is used to add resource files. The "Persistance" attribute has two options:
I) Link when compiling (compile-time Link ).
Ii) embed the resx file directly.
What is the difference?
I: When compiling. resx (an xml file) reads the relative path of the resource and finds the corresponding file based on the path (the Resources folder is automatically generated by default, it stores several resource files), converts them into binary form, and is compiled into exe together. This means that if another project needs to use this Resource file, because the Resource only stores a relative address (you do not copy the Resource file (folder) pointed to by this relative address, only copying a resource file may cause compilation errors because the corresponding image cannot be found during compilation ).
For type ii, directly convert the resource file into a binary code stream and embed it into the exe and Resources. Because the image metadata is directly embedded in the Resource, you do not need to use the real image. If you want to reuse the Resource in the future, you only need to copy the Resource file and add it to other projects, and then compile the reference. Www.2cto.com
So how do I read the resource files in Resources. resx? The first method is not feasible. We need to use the ResourceManager class: This class constructor has two versions of overload:
I) You need a type (Note: Resources. designer. cs is the class automatically generated by the resource file Resources. resx ).
Ii) A string is required (Note: The project Namespace root name is specified. [Folder name 1 ....... Folder name N]. Resouce file name, current Assembly ).
[Example 2]
A project has been created. The architecture diagram is as follows:
 
Now, read the image file embedded in the resourcefile (in the format of desert.jpg and Embeded in resx ).
[C #]
ResourceManager rm = new ResourceManager (typeof (Resources. Resources ));
// You can also:
// ResourceManager rm = new ResourceManager ("WinFormCSharp. Resources. Resources", Assembly. GetExecutingAssembly ());
BackgroundImage = (Bitmap) rm. GetObject ("Desert ");

[VB. NET]
Dim rm As New ResourceManager (GetType (Resources. Resources ))
'Can also:
'Resourcemanager rm = new ResourceManager ("WinFormCSharp. Resources. Resources", Assembly. GetExecutingAssembly ());
BackgroundImage = DirectCast (rm. GetObject ("Desert"), Bitmap)

In addition, you can use ComponentResourceManager to specify the class name of a Resouce file:
[C #]
ComponentResourceManager rm = new ComponentResourceManager (typeof (Resources. Resources ));
BackgroundImage = (Bitmap) rm. GetObject ("Desert ");

[VB. NET]
Dim rm As New ComponentResourceManager (GetType (Resources. Resources ))
BackgroundImage = DirectCast (rm. GetObject ("Desert"), Bitmap)

Maybe you will say-oh! In this case, the headers of various namespaces are too big-it doesn't matter. In fact, the reason why I first talk about this content is mainly to give us a clear understanding of Resouces. Microsoft has already considered this! Therefore, the easiest way is to directly
WinFormCSharp. Resources. Resources. This intelligently detects all resource files contained in Resources, which is of a strong type!

From Serviceboy

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.