An instance explanation. Creation and use of resource files in NET

xiaoxiao2021-03-06  55

An instance explanation. Creation and use of resource files in NET

First, resource files

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.

Second, the class used to create a resource file

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.

Third, the method of creating 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.

4. Method for adding resources to the resource file provides an AddResource () method in the ResourceWriter class, and the role of 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. Image image1 = image.fromfile ("abc1.jpg");

Image image2 = image.fromfile ("abc2.jpg");

RW.AddResource ("abc1", image1);

RW.AddResource ("abc2", image2);

(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 saving this resource file, and close the resource file, the specific: rw.close ();

5. Example creates a resource file

What kind of project is created here? Some friends may create a "console application" with .NET, there is no need to create a CS file directly with Notepad, if the name is: CreatResources.cs. Compile command: CSC CreatResources.cs; Run: CreatResources. A resource file called My.Resources is generated at this time. Put here first, wait for it.

Using system;

Using system.drawing;

Using System.Resources;

Class CreatResource

{

Public static void main ()

{

ResourceWriter RW = New ResourceWriter ("My.Resources");

Image image1 = image.fromfile ("abc1.jpg");

Image image2 = image.fromfile ("abc2.jpg");

RW.AddResource ("abc1", image1);

RW.AddResource ("abc2", image2);

Icon IC = New Icon ("CDDRIVE.ICO");

RW.AddResource ("abc3", IC);

Rw.addResource ("ABC4", "This is the character read from the resource file");

Rw.generate ();

Rw.close ();

}

}

6. Create a "Windows Application" test engineering to the generated resource file to create a "Windows Application" test project, add the My.Resources resource file to the project, steps below:

1. Select the file in the resource manager

2. Press and hold the left mouse button and drag to the project file, release the left mouse button.

3. Right click on the file in drag and drop, select "Properties"

4. Select "Embedded Resources" in the generation operation.

Seven, how to manage resources in the program in the program

In .NET Framework SDK, this provides a namespace for the creation and use of resource files - System.Resources. In this name space, there is a Class for ResourceManager. The main role of this Class is to manage and use the resource file. Visual C # is managed by this class and uses resources in the resource file in the embedder. The following code is to define a ResourceManager class to manage resources in embedded resource files:

ResourceManager RM = New ResourceManager ("res.my", askEMBLY.GETEXECUTINGASSEMBLY ());

Note: ResourceManager RM = New ResourceManager ("res.my", assembly.getexecutingassembly (); statement, the first parameter of the constructor is composed of two parts, and RES represents the namespace of the test engineering, my represents resources The root name of the file name my.resources, that is, the point number has a front part MY. Eight, how to use resources in the resource file in the program

In the previous article, we have learned that when you create a resource file, use the AddResource () method to join the resource, the first parameter in his syntax is the string that the user can define, this string is the resources. The unique identifier of the resource file is in the program design, is to use a resource through this unique identifier. So how do you get the required resources through this identifier in the program? This is to use the getObject () and getString () methods in the ResourceManager class. These two methods are to obtain the specified resource. Below is the syntax of these two methods:

Object getSting (String)

Object getObject (string)

"String" is the unique identifier of resources in the resource file. Careful readers may have noticed that the return value of these two methods is a variable of an Object type, that is, a variable of a reference type, and a string or image in the program, is a real value type variable. This needs to be converted, and this conversion is the boxes and out of the box above. The following code is extracting strings from resource files, images and icons resources:

Extract string resources:

String s = (String) RM.GetString ("mystr");

Extract icon resources:

Icon icodemo = ((icon) RM.GetObject ("Demo.ico");

Extract image resources:

Image a = ((image) (RM.GetObject ("OK-OFF.PNG")))))

Nine, test engineering source code Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

// Add two namespaces.

Using System.Resources;

Using system.reflection;

Namespace Res

{

///

/// Form1 summary description.

///

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.Forms.PictureBox PictureBox1;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.Button Button3;

Private system.windows.Forms.Label label1;

Private system.windows.Forms.Button Button4;

///

/// The required designer variable.

///

Private system.componentmodel.container components = null; public form1 ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.PictureBox1 = new system.windows.Forms.PictureBox ();

This.button1 = new system.windows.Forms.Button ();

This.Button2 = new system.windows.Forms.Button ();

This.Button3 = new system.windows.Forms.Button ();

THIS.LABEL1 = New System.windows.Forms.label ();

This.button4 = new system.windows.Forms.Button ();

THIS.SUSPENDLAYOUT ();

//

// PictureBox1

//

This.PictureBox1.Borderstyle = system.windows.Forms.Borderstyle.fixedsingle;

This.PictureBox1.Location = new system.drawing.point (8, 8);

This.PictureBox1.name = "PictureBox1";

This.Picturebox1.size = new system.drawing.size (256, 240);

This.PictureBox1.SizeMode = system.windows.Forms.PictureBoxSizeMode.stretchiMage;

This.PictureBox1.tabindex = 0;

This.PictureBox1.tabstop = false;

//

// Button1

//

This.Button1.Location = new system.drawing.point (280, 8);

This.button1.name = "button1";

This.button1.size = new system.drawing.size (88, 24);

This.button1.tabindex = 1;

This.Button1.Text = "Image 1";

This.Button1.click = new system.eventhandler (this.button1_click);

//

// Button2

//

This.Button2.Location = new system.drawing.point (280, 48);

This.button2.name = "button2"; this.button2.size = new system.drawing.size (88, 24);

This.button2.tabindex = 2;

This.Button2.text = "Image 2";

This.button2.click = new system.eventhandler (this.button2_click);

//

// Button3

//

This.Button3.Location = new system.drawing.point (280, 128);

This.button3.name = "button3";

This.button3.size = new system.drawing.size (88, 24);

This.button3.tabindex = 2;

This.Button3.Text = "text";

This.Button3.click = new system.eventhandler (this.button3_click);

//

// label1

//

THIS.Label1.Location = new system.drawing.point (8, 264);

THIS.LABEL1.NAME = "label1";

THIS.Label1.size = new system.drawing.size (360, 16);

THIS.Label1.tabindex = 4;

THIS.Label1.Text = "label1";

//

// Button4

//

This.Button4.Location = new system.drawing.point (280, 88);

This.button4.name = "button4";

This.button4.size = new system.drawing.size (88, 24);

THIS.BUTTON4.TABINDEX = 2;

This.button4.text = "icon";

This.button4.click = new system.eventhandler (this.button4_click);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

THIS.CLIENTSIZE = New System.drawing.size (376, 285);

This.Controls.add (this.label1);

This.Controls.add (this.button2);

This.Controls.add (this.button1);

This.Controls.add (this.PictureBox1);

This.Controls.add (this.button3);

This.Controls.add (this.button4);

THIS.NAME = "Form1";

THIS.TEXT = "Form1";

This.ResumeLayout (false);

}

#ndregion

///

/// The main entry point for the application.

///

[Stathread]

Static void main ()

{

Application.run (New Form1 ());

}

Private void button1_click (Object sender, system.eventargs e) {

System.Resources.ResourceManager RM = New ResourceManager ("res.my", assembly.getexecutingassembly ());

This.PictureBox1.Image = (bitmap) RM.GetObject ("abc1");

}

Private void button2_click (Object Sender, System.Eventargs E)

{

System.Resources.ResourceManager RM = New ResourceManager ("res.my", assembly.getexecutingassembly ());

THIS.PICTUREBOX1.Image = (Bitmap) RM.GetObject ("ABC2");

}

Private void button4_click (Object Sender, System.Eventargs E)

{

System.Resources.ResourceManager RM = New ResourceManager ("res.my", assembly.getexecutingassembly ());

THIS.ICON = (icon) RM.GetObject ("abc3");

}

Private void button3_click (Object Sender, System.Eventargs E)

{

System.Resources.ResourceManager RM = New ResourceManager ("res.my", assembly.getexecutingassembly ());

THIS.Label1.Text = rm.getstring ("abc4");

}

}

}

Ten, result map

XI, Source Code Download Website http://lizanhong.myshow.cn/

Twelve, thanks

Most of this article belongs to Wang Tian, ​​which is not necessary to write this article. So I reinited.

Thirteen, reference

http://www8.ccidnet.com/tech/guide/2002/01/22/92_3940.html

http://www.socent.com/dotnet/message.asp?id=218

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

New Post(0)