.NET resource file creation, use

xiaoxiao2021-03-06  103

The resource file is a file that stores resources (string, images, icons, etc.). The resource file is independent of the source program so that it can be used by multiple programs. At the same time, when the program is designed, sometimes the important thing is stored in the resource file for security or other factors, and the security and safety effect can be achieved.

Creating a resource file with Visual C # can always store three types of data resources, namely byte arrays, various objects, and strings, respectively.

One. Some concepts and theories used by Visual C # in creating resource files: In the .NET Framework SDK, one name is called System.Resources namespace, which provides many creation, storage, and use resource files for the application in this namespace. Class and interface. One of these is called ResourceWriter, and Visual C # is created and stored in the resource file by calling this class. two. Visual C # How to create a resource file: First of all, to inherit a resourceWriter class, then call a resourceWriter class generate (), you can generate a resource file. The specific statement is as follows:

ResourceWriter RW = New ResourceWriter ("My.Resources"); rw.generate ();

At this point, a resource file named "My.Resources" is generated in the disk, but the resource file at this time does not have anything, let's take a look at how to add resources to the resource file.

three. Add resources to resource files:

A addresource () method is provided in the ResourceWriter class, and this method is to add resources to the resource file. Different resources are different in Visual C #.

(1). Add byte array, grammar format is:

Public void addresource (string, byte []);

Note: Where String is the unique identifier of this byte array in the program when using the resource file

(2). Join the object, the grammatical format is:

Public void addresource (string, object);

Note: Where string is the unique identifier in the program when using the resource file

In this document, we use this call mode to join icons and images, as follows:

Icon ICO = New Icon ("Demo.ico"); Image Canceloff = Image.FromFile ("Cancel-Off); Image Cancelon = Image.FromFile (" Cancel-on.PNG "); Image CancelOver = Image. Fromfile ("ca Zan-over.png"); image okdown = image.fromfile ("ok-down.png"); image okoff = image.fromfile ("OK-OFF.PNG"); Image Okon = Image.FROMFile "ok-on.png"); rw.addResource ("Demo.ico", ICO); // Add Icon // below in the resource file to add image RW.AddResource in the resource file ("Cancel-OFF. PNG ", Canceloff); RW.AddResource (" Cancel-on.PNG ", Cancelon); rw.addresource (" ca Zanel-over.png ", ca Zan; rw.addresource (" ok-down.png ", okdown) rw.addResource ("OK-OFF.PNG", OKOFF); RW.AddResource ("OK-ON.PNG", OKON); (3). Add a string, the specific syntax is as follows:

Public void addresource (string1, string2);

Note: Where string1 is when using the resource file, this string is in the program in the program, in the program of this article:

Rw.addResource ("MyStr", "Read the string from the resource file!");

At this time, we have created a resource file, and several resources have been added in the resource file. Of course, you should pay attention to save this resource file, and turn off the resource file, as follows:

Rw.close ();

four. Create the source code code for the resource file:

Through the above discussion, we don't even understand the following code. The following program code is to create a name "My.Resources" resource file, and add an icon resource, several image resources, and a string resource in this resource file. The specific code is as follows:

CreatResources.cs: using System; using System.Drawing; using System.Resources; class CreatResource {public static void Main () {ResourceWriter rw = new ResourceWriter ( "My.resources"); Icon ico = new Icon ( "Demo.ico "); Image canff = image.fromfile (" ca Zanel-off.png "); image canage (" carative-on.png "); image canver = image.fromfile (" ca Zan-over.png ") Image okdown = image.fromfile ("OK-Down.png"); Image Okoff = Image.FromFile ("OK-OFF.PNG"); Image Okon = Image.FromFile ("OK-ON.PNG"); RW .Addresource ("Demo.ico", ICO); rw.addresource ("ca Zan-off.png", ca Zan; rw.addresource ("ca Zan-over.png", cazcelon; rw.addresource ("Cancel-over .png ", canvent); rw.addresource (" ok-down.png ", okdown; rw.addresource (" OK-OFF.PNG ", OKOFF); rw.addresource (" ok-on.png ", Okon ); rw.addresource ("mystr", "reading strings from the resource file!"); rw.generate (); rw.Close ();}} It is best to remind, in successful compilation into execution files After that, when this file is executed, you must ensure that the same directory of this execution file, there is an icon and image mentioned in the above code, otherwise it will be wrong when you create a resource file.

Fives. to sum up:

Creating a resource file with Visual C # is not a complex process, how to use the resource file in Visual C #, is the key and difficult points of Visual C # resource file programming.

PS: A: Is the resource file independent of EXE, DLL file?

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

New Post(0)