The example explains how to create and use. NET Chinese source files.

Source: Internet
Author: User

I. resource files
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.
Ii. Classes used to create resource files
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.
3. 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.
4. Add a resource 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 is the unique identifier of the object in the program when the resource file is used. For example: Copy codeThe Code is as follows: 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 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 ();
5. Create resource files in the example
What kind of project should I create here? Some friends may use. net to create a "console application". In fact, it is not necessary to directly create a CS file using notepad. For example, the name is CreatResources. cs. Compile command: csc CreatResources. cs; run: CreatResources. A resource file called My. resources is generated. Put it here and wait for it to be reused.Copy codeThe Code is 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 a character read from the resource file ");
Rw. Generate ();
Rw. Close ();
}
}

6. Add the generated resource file to the test project
Create a "Windows application" test project and add the My. resources resource file to the project. The steps are as follows:
1. Select a file in the resource manager.
2. Press and hold the left mouse button, drag it to the project file, and release the left mouse button.
3. Right-click the drag-and-drop file and select "properties"
4. Select "embedded resources" in the generation operation ".
7. 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 ("Res. My", Assembly. GetExecutingAssembly ());
Note: ResourceManager rm = new ResourceManager ("Res. my ", Assembly. getExecutingAssembly (); In the statement, the first parameter Res of the constructor. my consists of two parts. Res indicates the namespace of the test project, and My indicates the resource file name My. the root name of resources.
8. How to use resources in resource files in a program
The AddResource () method is used 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 the resource in the resource file. In programming, A resource is used by the unique identifier. 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 ")));
9. Test Project source code Copy codeThe Code is 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 of 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;
///
/// Required designer variables.
///
Private System. ComponentModel. Container components = null;
Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
//
// TODO: add Any constructor code after InitializeComponent calls
//
}
///
/// Clear all resources in use.
///
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
///
/// The designer supports the required methods-do not use the code editor to modify
/// Content 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. button#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 );
}
# Endregion
///
/// Main entry point of the application.
///
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}
Private void button#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.