Deep analysis of ASP.net resource files

Source: Internet
Author: User
Tags reflection resource
asp.net

  I. Definition of a resource document

The simple point is that the data that can be used in a program, such as String, picture, or binary data, can actually contain any type of file.

Resource files can have multiple language versions, which is important when developing globalized applications. This feature is accomplished by using the ResourceManager class in system.resources space to make the call to that version. Setting the property of a file to Embedded resource in vs.net allows the resource to be embedded in the assembly.

  What types of resource files are included?

System.Resources space supports three kinds of resource files:. TXT file, can only have string resources. Because it cannot be embedded in the Assembly, it is easily exposed and modified by the customer. The biggest disadvantage is that only string resources are supported, so it is not recommended. resx files, consisting of XML, that can be added to any resource, including binary. Also cannot be embedded in the assembly. A class that has a dedicated read-write in the System.Resources name space. Vs.net creates such a file and then converts it into a. resources file and embeds it into the Assembly according to the settings ... resources files, PE format, can be added to any resource. The only file that can be embedded in a Assembly is a class that has a dedicated read-write in the System.Resources namespace.

  Third, how to invoke the resource file?

The usual practice is to be. TXT file. resx files, etc. converted into. The resources file is invoked through ResourceManager, but we can also do it without that, and I find it more troublesome to do it as follows: We can embed a resource directly into Assembly without a resource file. This can be implemented in vs.net by setting the Build property of a file to Embedded Resource. In this case the ResourceManager is useless because it can only get the. resources file. So how do you call this kind of resource? We need to take advantage of some of the Reflection features. We just need to know some of the functions in the System.Reflection.Assembly class. There are three related functions, but we just need to assembly.getmanifestresourcestream this function. This function returns a resource embedded in assembly as a stream, and we can turn this stream into an object that is available in. NET. For example, if the embedded resource is a picture, then we can get the Bitmap object of the image resource using the Bitmap constructor of the New Bitmap (Stream); If we embed a text file, We can use StreamReader to read its contents.

Here's a simple demo with a text file as a resource

Step1: Resource File
Embed the file in the assembly and say how to do it
Step2: Calling a resource file
Public Stream Getembeddedresource ()
{
Return System.Reflection.Assembly.GetExecutingAssembly (). GetManifestResourceStream ("ResoureceDemo.test.txt");/the key sentence
}//resourecedemo.test.txt explain a ha
Resourecedemo represent namespaces, test.txt text file names
private void Button1_Click (object sender, System.EventArgs e)
{
Stream Sresponse;
StreamReader Srresponse;
Sresponse=getembeddedresource ();
Srresponse=new StreamReader (Sresponse);
String Txtdemo=srresponse.readtoend (). ToString ();
Label1. Text =txtdemo;
}

Of course, in the format of the picture is also the same operation. No, there's a tool in the end. Don't forget: Resgen.exe is dedicated to converting between resource file types. Supports conversion between. txt <->. resx <->. resources.







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.