Add resources to .NET assembly

xiaoxiao2021-03-06  119

In the .NET structure, the assembly assembly (also known as an assembly or hosted DLL) is a self-description installation unit, which can include only a PE (portable executable) format DLL or EXE file, or multiple File composition, such as resources (bitmap, JPEG files, string tables, etc.), DLL, and EXE, etc.

Among them, resources are a wide range of concepts, which can be composed of multiple elements, including interface elements (such as bitmaps, icons, or cursors), custom files required by the application, and version files installed using the data required for the application. , Menus and dialogs, etc. can be used as resources.

Resources can be reused, and the resources can be edited and modified as necessary. When we do this, it is best to separate these resource files, build assembly resources in a way similar to .NET module, so that resource files Independent in source code. In this way, it is possible not only to freely reference these resources in different procedures, but also makes non-professional programmers easily modify resources (of course, you can recompile them into new resources through programmers, or simple batch files. File), this independently modified to resource files does not trigger the impact on the assembly of references. At the same time, the use of resource files also helps to localize the application into different languages. The auxiliary program set is because only the resources are included without including program code, is widely used for the localization programming of the application.

The .NET structure provides a lot of support for resource files. Using the Visual Studio.NET integration development environment IDE easy to create a resource file, add the resource to the process of the project and add a form, the class library is as simple as the class library, the only difference is that you need to set the "buildaction" property "Embedded Resource" ", After this, you can use these resources.

Create a resource

From the above, you can know that the string table is a very common resource. To create such a resource file, there are two ways:

1), create it using the .NET command line tool ResGen. First create a text file containing resource content, which can use a general text editing software such as Notepad, EditPlus, etc. This text file consists of the desired "key value pair", the name of the key can be referenced in the program and set the string value to the creation of the file after setting the key name. As an example, the following segment generates such a resource, saved as a userinfo.txt file in the format below:

UserName = "SongH";

SEX = "boy";

BIRTHDAY = "1973-01-15";

SALARY = "5000RMB";

Then, convert the text file into resource files, which is still implemented through the ResGen tool. Perform the following statement: ResGen UserInfo.txt will generate a resource file Userinfo.Resources. In addition, ResGen can also create a .resx resource file based on an XML format, execute the following command resGenfo.resx will generate the resource userinfo.resx in XML format. However, the ResGen tool does not support the operation of the image resource, and the following method does not have such a limit.

2) use the ResourceWriter class

For easy to create a resource file, the .NET structure provides the ResourceWriter class to support the creation of various resource types such as images. The method containing the ResourceWriter class can write the resource to the output file or output stream in the system default format. Unless otherwise, it is not completed in one process here.

To create a resource file, call the resourceWriter class constructor initialization class instance and provide at least a traffic name or file name. The actual content of the resource is done by calling the AddResource method, and the AddResource method specifies the resource as name and value pair. The actual writing of resources requires calling the GeneRate method to implement, however, the Generate method is implicitly called when calling the Close method to turn off the ResourceWrit. ResourceWriter.AddResource () Method Adds resources to the list you want to write to the resource. After creating a ResourceWriter class instance, the method can add up to 2GB of resources, one of the overloaded methods is used to add String resources to the resource list:

Public void addresource

String name, // key name

String value // value

);

Here, the first parameter of the AddResource method specifies the key name, the second parameter specifies the value. Calling this method multiple times can complete the creation of the string table. Alternatively, add image resources can be implemented by instantiation (this time, add the System.Drawing namespace).

The following code segment generates a resource file UserInfo.Resources containing a string table and an image.

Using system;

Using System.Resources;

Using system.drawing;

Public Class RS

{

Public static void main ()

{

ResourceWriter RW = New

ResourceWriter ("UserInfo.Resources"); / / Provides file name to initialize the ResourceWriter class instance.

Image image = image.fromfile ("photo.gif"); // instantiate the image class

RW.AddResource ("photo", image); // Add image

RW.AddResource ("UserName", "SongH"); // Add a string

Rw.addResource ("SEX", "BOY"); // Add a string

RW.AddResource ("Birthday", "1973-01-15"); // Add a string

RW.AddResource ("Salary", "5000RMB"); // Add a string

Rw.close (); // Close ResourceWriter and implicitly calling the generate () method Complete the resource file to write disk files.

}

}

The above code first opens the graphic file photo.gif and creates an image object. In doing so, this graphic file must exist under the directory of the project executable (usually the item / bin / debug directory), or specify the full path to the image in the method parameter of Image.FROMFile (). Then, add a string resource to the ResourceWriter object several times a few times. Finally, call the close () method to turn off the ResourceWriter object and imply the generate () method to write the resource to file userinfo.resources.

Compile the above code and run it will create the resource file userinfo.Resources. The resource files generated above in the above can be added to the program set as an external file, or embedded in DLL or EXE. Let's continue to explain how to use the resource file in a Windows application.

Use resource files

To integrate development environment IDEs using the Visual Studio.net, you can easily add resource files to the assembly. Just add existing resource files in the created project, simply set its properties to embed the resource file into the assembly. The UserInfo.Resources resource file created above is illustrated by a C # Windows console instance. First, create a C # Windows Console project resourceuserinfo, open "Project / Add Existing Item", find the resource file UserInfo.resources in the previous creation to this project; then, select this resource file, set the property buildAction to Embedded Resource, this, the resource file can be embedded in the output. Now you can use this resource file. The ResourceManager class in the System.Resources namespace provides a way to easily access specific resources at runtime. Specifically, you can be implemented by getObject and getString methods, and the corresponding value will be returned by the key name parameter. The resourceManager class constructor is initialized a new instance of the ResourceManager class, and one of its overloaded methods lookups that are included in some files, which are exported from the specified root name using a given ASSEMBLY. Public ResourceManager

String BaseName,

AskEMBLY ASSEMBLY

)

Where parameter baseName indicates the root name of the resource. The root name is composed of an application namespace and a resource file name (without extension). Thus, the root name of the resource in this example should be: userinforeSource.UserInfo, can be programmed to obtain the name by calling the getManifestResourceNames () method.

Another parameter assembly represents the current main program set, the main set set in this example is actually the assembly that is being executed. Get a simple way to perform an assembly is to call the assembly.getexecutingAsembly () method.

After getting the ResourceManager instance, you can get the corresponding resources by specifying the key name.

The following table is part of the controls used in the program:

Category TextBoxTextBoxTextBoxTextBoxPictureBox Name UserNamesexbirthdaySalayPhoto

These controls can be dragged and dropped directly from the toolbox to the designer. Complete source code is:

Using system.reflection;

Using System.Resources;

Private system.resources.resourceManager RM;

Public Form1 ()

{

InitializationComponent ();

AskMBLY Assembly = askEMBLY.GETEXECUTISEMBLY (); // Get the current main assembly

RM = New ResourceManager ("ResourceUserInfo.uSerinfo", Assembly); // Instantiate Resource Management Class

PHOTO.IAMGE = (image) RM.GetObjetct ("photo");

Username.text = rm.getstring ("UserName");

Sex.text = rm.getstring ("sex");

Birthday.text = rm.getstring ("birthday");

Salary.text = rm.getstring ("salary");}

Run this code, you can take the content of the resource file.

转载请注明原文地址:https://www.9cbs.com/read-125829.html

New Post(0)