Visual C # resource file programming-create a resource file

Source: Internet
Author: User
A resource file is a file that stores resources. Resource files have their own unique advantages in programming. They are independent from source programs, so that resource files can be used by multiple programs. At the same time, during program design, important things are stored in resource files for security or other reasons, which can also achieve confidentiality and security. So what is stored in the resource file used by Visual C? Visual C # can be used to create resource files to store three types of data resources: byte arrays, various objects, and strings. This document uses a program example to illustrate how to create a resource file with Visual C.

1. concepts and theories used in creating resource files using Visual C:
In the. NET Framework SDK, a namespace named system. Resources provides many classes and interfaces for the application to create, store, and use resource files. One class is resourcewriter. Visual C # creates and stores resource files by calling this class.

Ii. Visual C # How to Create a resource file:
First, you must inherit a resourcewriter class, and then call a method of the resourcewriter class to generate a resource file. The statement is as follows:

Resourcewriter RW = new resourcewriter ("My. Resources ");
RW. Generate ();

In this case, a disk named "My. resources, but the resource file does not have any content at this time. Let's take a look at how to add resources to the resource file.

3. Add resources to the resource file:
The resourcewriter class provides an addresource () method to add resources to the resource file. In Visual C #, different resources are added in different ways.

(1) Add a byte array in the syntax format:

Public void addresource (string, byte []);

Note: string is the unique identifier of the byte array in the program when the resource file is used.

(2) Add an object in the syntax format:

Public void addresource (string, object );

Note: String indicates the unique identifier of the object in the program when the resource file is used.

In this program, we use this call method to add icons and images. The details are as follows:

Icon ICO = new icon ("demo. ICO ");
Image canceloff = image. fromfile ("cancel-off.png ");
Image cancelon = image. fromfile ("cancel-on.png ");
Image cancelover = image. fromfile ("cancel-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 the icon to the resource file
// Add images to the resource file
RW. addresource ("cancel-off.png", canceloff );
RW. addresource ("cancel-on.png", cancelon );
RW. addresource ("cancel-over.png", cancelover );
RW. addresource ("ok-down.png", okdown );
RW. addresource ("ok-off.png", okoff );
RW. addresource ("ok-on.png", Okon );

(3) Add a string. The syntax is as follows:

Public void addresource (string1, string2 );

Note: string1 is used in the resource file. The unique identifier of this string in the program is used in the program described in this article:

RW. addresource ("mystr", "Read string from resource file! ");

At this point, we have created a resource file and added several resources to the resource file. After that, you should also note that you should save the resource file and close the resource file, the details are as follows:

RW. Close ();

4. source code for creating resource files:
Through the above discussion, we can easily understand the following code. The following program code is used to create a resource file named "My. Resources" and add an icon resource, several image resources, and a string resource to the resource file. The 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 canceloff = image. fromfile ("cancel-off.png ");
Image cancelon = image. fromfile ("cancel-on.png ");
Image cancelover = image. fromfile ("cancel-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 ("cancel-off.png", canceloff );
RW. addresource ("cancel-on.png", cancelon );
RW. addresource ("cancel-over.png", cancelover );
RW. addresource ("ok-down.png", okdown );
RW. addresource ("ok-off.png", okoff );
RW. addresource ("ok-on.png", Okon );

RW. addresource ("mystr", "Read string from resource file! ");
RW. Generate ();
RW. Close ();
}
}

It is best to remind you that after the file is successfully compiled into an execution file, you must ensure that the same directory of the execution file exists in the icons and images mentioned in the above Code, otherwise, an error occurs when creating the resource file.

V. Summary:
It can be seen that using Visual C # To create a resource file is not a complex process. In the next article, we will introduce how to use resources in the resource file in Visual C. This is the focus and difficulty of Visual C # resource file programming. Of course, the resource file used in this article is the resource file created in this article.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.