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.