Load files from resource files or read resources (C #)

Source: Internet
Author: User

Load files from resource files (C #)

 

Purpose:

1. During multi-language processing (different translated texts are usually stored in the resource package .)

2. Package the image into the DLL (icon, custom control, etc ..)

 

Resource file format:. resx |. Resources

Package resource files:

System. Resources. resourcewriter RW = new resourcewriter (@ ".../../ABC. Resources ");

RW. addresource ("ABC", new byte [1, 10000000]);

RW. Generate ();

RW. Close ();

Note:

Create a resourcewriter object for writing resource files. The file suffix must be. Resources, which cannot be identified by other systems. Then, the resource information is written by addresource. This information can be a string, a byte array, or even an object (image ). Generate and close are added. After running the program, the resource file is created.

If you do not need to package the resource file into the EXE or DLL program, you can use system. Resources. resourcereader to read the resource file information.

 

Obtain resource files

After the resource file is packaged, the next step is to integrate the resource file into the project. There are two methods. One is to compile the project in the console, and load the resource into the EXE by loading the resource file. The other is much simpler, it is to load the resource file directly into the project. vs identifies the file to be loaded based on its suffix as a resource file, and automatically packs the file into the resource file during compilation. If the resource file is not added to the EXE file after compilation, you can set the attribute of the resource file and set the attribute of build action to embedded resource.

 

Read resources from the current project:

This. Icon = properties.resources.exe icon; |

This. Text = resource1.string1. tostring (); // mainly a string

 

After the resource file is successfully added to the EXE file, the next step is to read the resource file in the program.

System. Resources. ResourceManager Rm = new system. Resources. ResourceManager ("resunce. ABC", this. GetType (). Assembly );

Byte [] bit = RM. GetObject ("ABC") as byte [];

Note:

It is usually used to create a ResourceManager object. The first parameter is the name of the resource. Note that "resunce" is the name of the component (namespace) during compilation, "ABC" is the name of the resource file. If you did not compile the project file through vs, but directly compile the project file through the console, the resource name does not need to contain the component name, but "ABC. Is it possible that vs has some parameters (the component name is determined) during compilation, so the component name must be included when the project reads the resource file.

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.