C # resource file

Source: Internet
Author: User
Visual C # resource file programming-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
Sometimes, for security or other reasons, storing important things in resource files can also achieve confidentiality and security. Then visual
C # What is stored in the resource file used? When using Visual
C # creating resource files can basically store three types of data resources: byte arrays, various objects, and strings. This document describes how to use visual
C # How to Create resource files.

1. concepts and theories used in creating resource files using Visual C:


In the. NET Framework
The SDK is named system. resources. In this namespace, many classes and interfaces are provided for the application to create, store, and use resource files. There is a class
The name 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.

Visual C # resource file programming-use resource files

This article will go on to the topic of the previous article to discuss another question about resource files.
C # How to use resource files. In the previous article, we have successfully created a resource file named "My. resources. This resource file contains an icon resource.
The file name is "demo. ICO", several image resources and a string resource, and the file name is "mystr ". We will use the resource file created in the previous article as the object
A specific program example shows how to use resource files 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. This is the so-called real-value type variable (value type ).
Variable) and reference type variable ). And the mutual conversion between the two, in the visual
C # is called boxing and unboxing ). The so-called packing is the process of converting a real-value type variable into a reference type variable, and vice versa. So what type of Variable
It is a reference type variable. What type of variable is a real value type variable? What is the difference between the two? In the visual
The reference types in C # refer to these types, such as object, class, interface, Delegate, string, and array. The
A variable is a reference type variable. 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 the reference type change
A volume is a pointer to an object, and a real value type variable is a real object. Since packing and picking are very conceptual operations, it takes a lot of time to describe in detail. This is beyond the scope of this article
Main category. 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
"/Resource" command, which can be abbreviated as "/Res ". In this article, the program name is "use. cs", the name of the resource file
For "My. Resources", the compilation command for embedding resources into the program is as follows:

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

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

In 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 (
) Method to add the resource. The first parameter in his syntax is a string that can be defined by the user. This string is the unique identifier of the resource in the resource file. In programming, is to use this unique identifier to make
Use a resource. So how can we get the required resources through this identifier in the program? This requires the GetObject () and
Getstring () method. 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
Variable of reference type, and the string or image in the program is a real-value variable. This requires conversion, and this conversion is the packing and output mentioned above. The following code is from the resource file:
Extract strings, images, and ICON resources:

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
The image resource in the source file. Another function is to display the string resource 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 ")));
}
}

Http://blog.csdn.net/kenkao/archive/2009/03/16/3994543.aspx
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!

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.