Visual C # Resource File Programming - Create a resource file

xiaoxiao2021-03-06  119

The resource file as the name implies is the file that stores resources. Resource files have their own unique advantages in programming, and he is independent of the source program so that the resource file 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. So what is the resource file used by Visual C #? Three types of data resources can be stored in the use of Visual C #, which are byte arrays, various objects, and strings, respectively. This article will specifically explain how to create a resource file with Visual C # in conjunction with a program example.

One. Some concepts and theories used by Visual C # in creating resource files:

One name in the .NET Framework SDK is called System.Resources namespace, which provides a number of creation, storage, and use resource files, and interfaces for applications in this namespace. 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 inherit a resourceWriter class, then call a method generate () that generate () 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. Summary: The visible Visual C # Creating a resource file is not a complex process. In the next article, we will show you how to use the resource files in Visual C #. This is the focus and difficulties of Visual C # resource file programming. Of course, the resource files used in the text are the resource files created in this article.

Original link: http://www0.ccidnet.com/tech/guide/2002/01/21/92_3939.html

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

New Post(0)