Examples to explain. NET resource file creation and use _ Practical skills

Source: Internet
Author: User
I. Resource document
A resource file, as its name suggests, is a file that holds resources. Resource file in the program design has its own unique advantages, he is independent of the source program, so that resource files can be used by multiple programs. At the same time, in the program design, sometimes for security or other factors, the important things stored in the resource file, can also achieve confidentiality, security effects. So what exactly is stored in the resource file that Visual C # uses? Creating a resource file in Visual C # roughly holds three types of data resources, namely byte arrays, various objects, and strings. This article combines a program example to specify how to create a resource file with Visual C #.
second, the class used to create the resource file
In the. Net FrameWork SDK, a name is called the System.Resources namespace, which provides many classes and interfaces for the application to create, store, and use resource files in this namespace. One of the classes called Resourcewriter,visual C # is to create and store resource files by calling this class.
How to create a resource file
You can generate a resource file by inheriting a ResourceWriter class and then invoking a method generate () of the ResourceWriter class. The specific statements are as follows:
ResourceWriter rw = new ResourceWriter ("My.Resources");
rw. Generate ();
A resource file called "My.Resources" is generated in the disk at this time, but there is nothing in the resource file at this time, so let's look at how to add resources to the resource file.
Iv. ways to add resources to a resource file
A AddResource () method is provided in the ResourceWriter class that is used to add resources to the resource file. There are different ways to add different resources in Visual C #.
(1). Add byte array, syntax format:
public void AddResource (string, Byte []);
Note: Where string is the unique identifier of this byte array in the program when the resource file is used
(2). Add object, the syntax format is:
public void AddResource (String, object);
Note: Where string is the unique identifier of this object in the program when the resource file is used. Such as:
Copy Code code as follows:

Image image1 = Image.FromFile ("abc1.jpg");
Image image2 = Image.FromFile ("abc2.jpg");
rw. AddResource ("ABC1", Image1);
rw. AddResource ("ABC2", image2);

(3). Add string, the specific syntax is as follows:
public void AddResource (string1, string2);
Note: Where string1 is used as a resource file, the unique identifier for this string in the program is used in the program in this article:
rw. AddResource ("MyStr", "read the string from the resource file!) " );
Now that we have created a resource file and added several resources to the resource file, you should also note, after this, that you save the resource file and close the resource file as follows:
rw. Close ();
v. Example create a resource file
What kind of project would you like to create here? Some friends may use. NET to create a "console application", in fact, there is no need, directly with Notepad to create a CS file on it, if the name is: CreatResources.cs. Compile command: csc CreatResources.cs; run: creatresources. A resource file called My.Resources is generated. First put it here, wait for it.
Copy Code code as follows:

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 ();
}
}

vi. Add the generated resource file to the method in the test project
Create a "Windows Application" test project, add the My.Resources resource file to the project, and follow these steps:
1, select the file in Resource Manager
2, hold down the left mouse button, drag to the project file, release the left mouse button.
3, click the right mouse button on the dragged file, select "Properties"
4, select "Embedded Resource" in the build operation.
Vii. How to manage resources in a resource file in a program
In the. Net FrameWork SDK, this provides a namespace--system.resources for resource file creation and use. In this namespace there is a class for ResourceManager, and the primary function of this class is to manage and use resource files. This class is used by Visual C # to manage and use resources in the resource files in the embedded program. The following code defines a ResourceManager class to manage resources in an embedded program resource file:
ResourceManager rm = new ResourceManager ("res.my", assembly.getexecutingassembly ());
Note: ResourceManager rm = new ResourceManager ("res.my", assembly.getexecutingassembly ()); In the statement, the first parameter of the constructor is res.my by a two-part structure Into, res represents the namespace of the test project, my name is the root of the resource file name My.Resources, and the point number has the previous part of my.
Viii. How to use resources in a resource file in a program
Using the AddResource () method to add resources, the first parameter in his syntax is a string that the user can define, a string that is the unique identifier of a resource in a resource file, in the program design, by using a unique identifier to use a resource. So how do you get the resources you need through this identifier in your program? This will use the GetObject () and GetString () methods in the ResourceManager class. These two methods are used to obtain the specified resources. The following are the syntax for these two methods:
Object Getsting (String)
Object GetObject (String)
The "String" is the unique identifier of the resource in the resource file. Attentive readers may have noticed that the return value of these two methods is a variable of type object, that is, a variable of a reference type, while a string or image in the program is a real-value type variable. This requires a conversion, which is the boxing and out of the box described above. The following code extracts the string, image, and icon resources from the resource file:
Extract string resource:
string s = ((String) rm. GetString ("MyStr"));
Extract icon resource:
Icon Icodemo = (icon) RM. GetObject ("Demo.ico"));
Extract Image resources:
Image a = ((Image) (rm. GetObject ("Ok-off.png"));
ix. test Project source code
Copy Code code as follows:

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Add the following two namespaces.
Using System.Resources;
Using System.Reflection;
Namespace Res
{
///
Summary description of the Form1.
///
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 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Code generated #region the Windows forms Designer
///
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
///
private void InitializeComponent ()
{
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 = "writing";
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);
}
#endregion
///
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");
}
}
}

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.