Read methods for embedding and using resource files in. NET _c# Tutorial

Source: Internet
Author: User

There are two ways to embed resources (bitmaps, icons, or cursors, etc.) in dotnet, one is to add the resource file directly to the project as an embedded resource and get the stream of the resource through the assembly GetManifestResourceStream method in the code. Another way is to add to the project. ResX resource files, add resources to the resource file, and manage the resources in the ResourceManager class uniformly.

The two methods are detailed below

First, use GetManifestResourceStream to read embedded resources

1. Add the resource file

Adding a resource file that you want to embed into an assembly directly into your project can be added to the project's root directory and can be added to any directory in your project.

2, set the resource file "BuildAction" property

Set the BuildAction property of the embedded resource file to "Embedded Resource"

3, the use of embedded resources in the code

Gets the namespace

type type = Methodbase.getcurrentmethod () where the class is running. DeclaringType;

String _namespace = type. Namespace;

Get the current running Assembly

Assembly _assembly = assembly.getexecutingassembly ();

Generate resource names based on namespace and filename

string resourcename = _namespace +. Directory. Bitmapmanifest.bmp ";

Gets the stream stream stream of this resource from assembly, based on the resource name

= _assembly. GetManifestResourceStream (resourcename);

Image myimage = Image.fromstream (stream);

Note the rules for the composition of resource names:

Project default namespace. The directory in which the resource resides in the project. resource File name

The bitmapmanifest.bmp bitmap in the code above is in the project's directory directory. The project default namespace if the resource file is directly in the project root directory. resource file name

Give an example of an XML resource:

Gets the namespace

type type = Methodbase.getcurrentmethod () where the class is running. DeclaringType;

String _namespace = type. Namespace;

Get the current running Assembly

Assembly _assembly = assembly.getexecutingassembly ();

Generate resource names based on namespace and filename

string resourcename = _namespace + ". Xmlfiletest.xml ";

Gets the stream stream stream of this resource from assembly, based on the resource name

= _assembly. GetManifestResourceStream (resourcename);

 

XmlDocument xmldoc = null;

xmldoc = new XmlDocument ();

Xmldoc.load (stream);

Second, use. ResX resource file Embedded Resource

1. New Resource file

Creates a new resource file in the project, the resource file suffix. resx, and a new Designer.cs file with the same name as the resource file.

In fact, the biggest use of resource files is to use the language version of the software to save the resources of different languages, such as the menu text in different languages, you can put the strings of different languages in the same resource type under the different resource bundles, The program runtime selects different packages to display strings of different languages based on the culture of the run-time system.

You can add a resource file to a resource file after you create a new resource file:


Figure 1 Resource types that the RESX resource file can embed

Resources can include resources for strings, bitmaps, icons, audio, files, and so on.

The added resource is saved in the project's Resources folder.

2, set the resource file "BuildAction" property

The "BuildAction" property of all resource files in the Resources folder is set to Embedded Resource.

3. Resource existence mode

The resources managed by the. resx resource file can exist in two forms, one in the resource folder as a normal file, and the other in the. resx resource file after Base64 encoding.


Figure 2. How resources exist in a RESX resource file

Open the. resx resource file, select the resource, and persistence the attribute in the attribute to determine the existence of the resource. The two forms of existence of a resource are called the same in code.

4, the use of embedded resources in the code

Gets the namespace

type type = Methodbase.getcurrentmethod () where the class is running. DeclaringType;

String _namespace = type. Namespace;

Gets the current main assembly

Assembly currentassembly = assembly.getexecutingassembly ();

The ROOT name of the resource

string resourcerootname = _namespace + ". ResourceTest ";

Instantiate resource management class

ResourceManager ResourceManager = new ResourceManager (Resourcerootname, currentassembly);

Obtain resource object according to resource name

icon Myicon = (icon) resourcemanager.getobject ("Icontest");

Note the rule for the root name of the resource:

The ROOT name of the resource is the full name of the resource file class.

Open the. resx resource file The corresponding Designer.cs file can see the full name of the resource class: namespace. Resource class name.

The resource name is the name of a resource in the. resx resource file.

Open the. resx resource file to see the names of the individual resources. The name of the icon file Icontest.ico in the figure above is icontest.

To retrieve a "string" resource, call the GetString method.

To retrieve other types of resources, call the GetObject method and explicitly convert the resulting resource to the corresponding type.

5. Multi-language Resource application

Get the current language environment
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;

Icon Myicon = (icon) resourcemanager.getobject ("Icontext", CI);

The above is the cloud habitat community for everyone to organize the two kinds of embedding and use of resource files, I prefer to use the second, I hope to help everyone.

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.