Managed Resources Full Introduction

Source: Internet
Author: User
Tags resource

This article applies to all languages that use the. NET v1.1 and above frameworks.

Prequel 1: Ask a question (a reader who is anxious to know the official content, please go to the ^_^ section below)

Some time ago wrote an article entitled "VC." NET easy to implement button control from the article, where the button pressed when the effect is presented by a picture. Then the question came, and I initially added the picture to the resource list (. rc), and then I used the following 2 methods to read the resource; The compilation passed, and the program threw an exception, prompting that the resource file could not be found.

//方法1,从指定的hinstance中名为bitmapName的资源创建Bitmap对象
public: static Bitmap* System::Drawing::Bitmap::FromResource(IntPtr hinstance,String* bitmapName);
//方法2,使用构造函数从指定的type类中提取的resource资源初始化Bitmap对象
public: System::Drawing::Bitmap::Bitmap(Type* type,String* resource);    

Prequel 2: Analyze the problem then I am puzzled, in the forum to raise the question, did not get a constructive answer, in order to rush the time had to hastily put the pictures and programs released together. After hindsight, the heart is very not the taste of the separation of resources and program files released together, make a mess of the directory is not good to see why the picture of the inclusion of resources can not use it? Will the resources not be packaged into the. exe file? So I used the 2-view tool to open the resource picture and the. exe file, and made a comparison, and found that the resource picture was packaged into the. exe file:

Although the resource has been packaged into an. exe file, there seems to be no other function to read the internal resources, except for the two methods mentioned above.

Prequel 3: See the dawning of the problem solving

Open MSDN to find the index on resources, and look at it and finally figure it out, the original. NET Framework no longer supports resources that are packaged into programs through the resource Editor (. rc), instead a resource called managed resources. So what are the similarities and differences between this resource and ordinary unmanaged resources? Since you cannot package resources using the Resource Editor (. rc) provided by Visual Stdio 200x, how should this resource be packaged into a program? How do you use it when you have a successful package?

Story 1: What is a managed resource

Resources can be understood to be any of the executable data that is logically deployed by the application, and the nature of the managed resource is probably the same. So what is the difference between the RC resources that MFC uses and the managed resources that. NET uses? Take a look at the following table:

95/98/me/nt/2000 and above

Resource is found on resource ID

No

Using a pure resource DLL is more cumbersome

Determined by the programmer, Management chaos

Have not thought well:)

There is currently no support for

To cancel the resource ID, depending on the resource name, or on the value of the resource (content)

Serializable can be

Use a dedicated resource file, very simple

Unified Administration by Assembly (note 2)

Note 1: For example, to a folder right select the "property"-> "Custom"-> "Change Icon" in "Browse" select the. exe file, if supported by the system will read out all the icons packaged in the. exe, whereas only 1 can be read;

Note 2: Discussion of assemblies is beyond the scope of this article, and interested readers can refer to MSDN; As you can see from the table above, although the essence of managed resources is not the same as the "resources" of the past, the way it is used is completely overturned. A managed resource treats a resource as an object, a lookup reference to a resource no longer uses the resource ID, instead of the resource name, as if using an object, and conversely, a good name for any serializable object can be saved as a resource.. NET managed resources are packaged in two ways, Are the resources that are packaged into the program and separate resource files. Independent resource files are organized in two forms, one is the XML file with an extension of ResX, which is organized in XML text, and the other is a 2-based file with the extension resources, which is organized in a 2-way system.

. ResX

Text file composed of XML tags

The volume of the text is, of course, very large.

Text resources are unchanged, other objects are translated into text using Base64 algorithm

When writing. NET forms programs vs. resx files, save resources by packaging resources in ResX into. exe files at compile time, while generating the. resource file

. resources

2-in file consisting of 2 binary data

It's a lot smaller than an XML file with the same content.

Text resource converted to 2 data, other objects unchanged

Story 2: Operations on managed resources

The operations classes in the following several system::resources namespaces can basically satisfy all resource operations:

Direct index or enumerate XML resource (. resx) files or resources in the stream
resxresourcereader () enumeration XML resource (. resx) files or resources in the stream
resxresourcewriter () write resource to. resx file or output stream
resourceset () direct index or enumerate. resources file and resource in stream or stream
resourcereader () enumeration. resources file and resource in stream or stream
resourcewriter () write resource to. resources file or output stream

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.