Visual C # Resource File Programming - Create a resource file

zhaozj2021-02-16  109

Visual C # Resource File Programming - Create a resource file

(Wang Tian, ​​January 21, 2002 23:46) Resource Document As the name implies is the documentation of the resource. 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: 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 time, a resource file named "My.Resources" is generated in the disk, but at this time there is no content, Let's take a look at how to add resources to your resource file. three. Add a resource in the resource file: A AddResource () method is provided in the ResourceWriter class, which is the role of this method 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 (2) of this byte array in the program when using the resource file. Join the object, the syntax format is:

Public void addresource (String, Object); Note: Where String is when using the resource file, this object is in the program in this program, and we use this call to add icons and images, the specific :

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-OK.PNG", OKON); (3). Add a string, the specific syntax is as follows: public void addresource (String1, String2 ); Note: When String1 is when using the resource file, this string is in the program in the program, which is used in the program of this article: rw.addResource ("MyStr", "reading from the resource file String! "); Earth, 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. Source of 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:

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

New Post(0)