C # load files from resource files

Source: Internet
Author: User
1. Overview:
 
Resource files have been available for a long time. This is not uncommon in the net era.
Generally, different translated texts are stored in the resource package. In the past, resource files can be stored in addition to strings. However, in the C # project, the Resource Management Panel is empty (the resource panel in the VC ++ project can be used), and resource attributes cannot be directly edited. However, each winform will contain one. resx file. This file is the winform resource file. If you add a resource file to the project by adding a new project, the. resx file is generated. It is estimated that the. resx file is a. net resource file format designed for C .. Resx is saved in XML format and mainly stores strings. I have not found the method for adding files to. resx. Therefore, we need to use other methods to package the files into the resource file.
 
2. Package resource files
 
. Net in addition to the. resx resource file format, there is also a. Resources resource file format. It supports file loading. The common practice is as follows:
System. Resources. resourcewriter RW = new resourcewriter (@ ".. \ ABC. Resources ");
RW. addresource ("ABC", new byte [1, 10000000]);
RW. Generate ();
RW. Close ();
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.
 
3. 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.
 
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 [];
 
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.
 
4. Resource file packaging Tool
Loading a file in a resource file involves two operations: creating a resource file and reading a resource file. You can use the resource packaging tool to create a resource file without any special requirements. The example program in the SDK has a packaging tool that can be used for compiling. Compilation address: Program Files \ Microsoft Visual Studio. NET 2003 \ SDK \ V1.1 \ samples \ tutorials \ resourcesandlocalization \ reseditor
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.