C # create and use resource files

Source: Internet
Author: User

Create a resource file

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.

Use resource files

In Visual C # resource file programming-create resource file, we have introduced how to use Visual C # To create resource files. This article will go on to the topic of the previous article to discuss another question about resource files, how to use resource files in Visual C. In the previous article, we have successfully created a resource file named "My. resources. This resource file contains an icon resource named "demo. ICO", several image resources and a string resource, and the name "mystr" in the file ". We will take the resource file created in the previous article as an object, and take a specific program example to see how to use the resource file with Visual C.
I. software environment for program design and operation in this article:
(1). Microsoft Windows 2000 Server Edition

(2). Net Framework SDK beta 2

Ii. Some concepts and theories in programming:
The concept and theory involved in programming are mainly the mutual conversion of two variables. These two variables are called value type variable and reference type variable ). The conversion between the two is called boxing and unboxing in Visual C ). The so-called packing is the process of converting a real-value type variable into a reference type variable, and vice versa. So what types of variables are reference type variables and what types of variables are real-value variables? What is the difference between the two? The reference types in Visual C # generally refer to these types, such as object, class, interface, Delegate, string, and array. The variables defined by these types are reference type variables. The real value type is the common integer, Boolean, and enumeration types. The variables defined by these types are the real value type variables. The biggest difference between them is that reference type variables are pointers to object objects, and real-value type variables are real object objects. Since packing and picking are very conceptual operations, it takes a lot of time to describe in detail. This is beyond the main scope of this article. Therefore, this article only introduces Program-related operations. The detailed procedure will be introduced in the next session.

Iii. Solutions to the important steps of programming:
(1). How to embed resources during program compilation. The resource file and program are two independent files. to embed the resource file into the final generated program, you must add the "/resource" command during compilation, this command can be abbreviated as "/Res ". In this article, the program name is "use. cs" and the resource file name is "My. Resources". The compilation command of the resource embedding program is as follows:

Csc.exe/Res: My. Resources Use. CS

(2). How to manage resources in resource files in a program:

The. NET Framework SDK provides a namespace for creating and using resource files-system. Resources. In this namespace, a class is ResourceManager. The main function of this class is to manage and use resource files. Visual C # uses this class to manage and use resources in the resource files embedded in the program. The following code defines a ResourceManager class to manage resources in the embedded program resource file:

ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());

(3). How to use resources in resource files in a program:

In the previous article, we learned that when creating a resource file, we used the addresource () method to add resources. The first parameter in his syntax is a string that can be defined by the user, this string is the unique identifier of a resource in a resource file. In programming, it is used to use a resource. So how can we get the required resources through this identifier in the program? This requires the GetObject () and getstring () methods in the ResourceManager class. These two methods are used to obtain the specified resource. The syntax of these two methods is as follows:

Object getsting (string)
Object GetObject (string)

The "string" is the unique identifier of the resource in the resource file. Careful readers may have noticed that the return values of these two methods are all object-type variables, that is, a reference-type variable, and strings or images in the program, is a real-value type variable. This requires conversion, and this conversion is the packing and output mentioned above. The following code extracts strings, images, and ICON resources from resource files:

Extract string resources:

String S = (string) Rm. getstring ("mystr "));

Extract ICON resources:

Icon icodemo = (icon) Rm. GetObject ("demo. ICO "));

Image extraction resources:

Image A = (image) (RM. GetObject ("ok-off.png ")));

4. Take a program example to see how to use the resource file:
The resource file used in the following program example is the resource file created in the previous article "My. resources ", the program defines three lable components, two of which are used to display image resources in the resource file, and the other is to display string Resources in the resource file, the program icon is taken from the icon resource in the resource file. The following is the source code of the program:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. Resources;
Using system. reflection;
Public class form1: Form
{
Private Label lblok;
Private Label lblcancel;
Private system. componentmodel. Container components = NULL;
Private Label lblresource;
Public form1 ()
{
// Initialize components in the form
Initializecomponent ();
}
// Clear resources used in the program
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
Private void initializecomponent ()
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblok = new label ();
This. lblcancel = new label ();
This. lblresource = new label ();
This. suspendlayout ();

This. lblok. backcolor = system. Drawing. color. White;
// Use image resources in the resource file
This. lblok. Image = (image) (RM. GetObject ("ok-off.png ")));
This. lblok. Location = new system. Drawing. Point (24, 40 );
This. lblok. Name = "lblok ";
This. lblok. size = new system. Drawing. Size (75, 23 );
This. lblok. tabindex = 0;
This. lblok. Click + = new system. eventhandler (this. lblok_click );
This. lblok. mouseenter + = new system. eventhandler (this. lblok_mouseenter );
This. lblok. mouseleave + = new system. eventhandler (this. lblok_mouseleave );
// Export
// Use image resources in the resource file
This. lblcancel. Image = (image) (RM. GetObject ("cancel-off.png ")));
This. lblcancel. Location = new system. Drawing. Point (152, 40 );
This. lblcancel. Name = "lblcancel ";
This. lblcancel. size = new system. Drawing. Size (75, 23 );
This. lblcancel. tabindex = 1;
This. lblcancel. Click + = new system. eventhandler (this. lblcancel_click );
This. lblcancel. mouseenter + = new system. eventhandler (this. lblcancel_mouseenter );
This. lblcancel. mouseleave + = new system. eventhandler (this. lblcancel_mouseleave );

This. lblresource. Location = new system. Drawing. Point (88, 8 );
This. lblresource. Name = "lblresource ";
This. lblresource. tabindex = 2;
// Export
// Use the string resource in the resource file
This. lblresource. Text = (string) Rm. getstring ("mystr "));

This. autoscalebasesize = new system. Drawing. Size (5, 13 );
This. clientsize = new system. Drawing. Size (240,101 );
This. Controls. Add (lblresource );
This. Controls. Add (lblcancel );
This. Controls. Add (lblok );
// Export
// Use the icon resource in the resource file
Icon icodemo = (icon) Rm. GetObject ("demo. ICO "));
This. Icon = icodemo;
This. Name = "form1 ";
This. Text = "Visual C # use resource files! ";
This. resumelayout (false );

}
Static void main ()
{
Application. Run (New form1 ());
}
Private void lblok_mouseenter (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblok. Image = (image) (RM. GetObject ("ok-on.png ")));
}

Private void lblok_mouseleave (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblok. Image = (image) (RM. GetObject ("ok-off.png ")));
}

Private void lblok_click (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblok. Image = (image) (RM. GetObject ("ok-down.png ")));
}

Private void lblcancel_mouseenter (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblcancel. Image = (image) (RM. GetObject ("cancel-onr.png ")));
}

Private void lblcancel_mouseleave (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblcancel. Image = (image) (RM. GetObject ("cancel-off.png ")));
}

Private void lblcancel_click (Object sender, system. eventargs E)
{
ResourceManager Rm = new ResourceManager ("Images", assembly. getexecutingassembly ());
This. lblcancel. Image = (image) (RM. GetObject ("cancel-over.png ")));
}
}

V. Summary:
So far, we have completed all the programming in Visual C # resource files. The main content is to create resource files and use resource files, I want to introduce these two articles. You should have a comprehensive understanding of resource files!

Related Article

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.