C # Create and use resource files

Source: Internet
Author: User
Tags reference resource
Create

Create a resource file

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 #.

A Some concepts and theories used in Visual C # to create resource files:
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.

Two How Visual C # creates 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.

Three To add a resource 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

In this procedure, we use this calling method to add icons and images, 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 icon to resource file
The following is the inclusion of images in a 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 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 ();

Four To create the source code for a resource file:
Through the above discussion, it is not difficult to understand the following code. The following program code works by creating a resource file called "My.Resources" and adding an icon resource, several image resources, and a string resource to the resource file. The specific 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 the string from the resource file!) " ) ;
rw. Generate ();
rw. Close ();
}
}

It is also a good idea to remind you that after successfully compiling the file, you must make sure that the same directory in which the file is executed, the icon and the image mentioned in the code above are executed, otherwise there will be errors in creating the resource file.

Five Summarize:
It is not a complicated process to create a resource file with Visual C #, and in the next article we'll show you how to use resources in a 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 paper.

Working with resource files

In the Visual C # resource file programming-Creating a resource file-you have described how to create a resource file with Visual C #. This article will go on to the topic of the previous article to explore another problem with resource files, how to use resource files in Visual C #. In the previous article, we have successfully created a resource file called "My.Resources". This resource file contains an icon resource with the name "Demo.ico" in the file, a number of picture resources, and a string resource in the file with the name "MyStr". We have created this resource file as an object, combining a specific program example to see how to use a resource file with Visual C #.
A The software environment for the design and operation of this article:
(1) Microsoft company Windows 2000 Server Edition

(2). Net FrameWork SDK Beta 2

Two Some concepts and theories in programming:
The concepts and theories involved in programming are mainly the conversion of two kinds of variables. This is what two variables are called real value type variables (value type Variable) and reference type variables (Reference type Variable). The conversion between the two is referred to as boxing (Boxing) and out of box (unboxing) in Visual C #. Boxing is the process of converting a real-value type variable into a reference type variable, whereas the other is out of the box. So what type of variable is a reference type variable, and what type of variable is a real value type variable? What's the difference between the two? The reference type in Visual C # refers to these types, such as Object, Class, Interface, Delegate, String, array, and so on. The variables defined by these types are reference type variables. The real value type is the usual normal use of Integer, Boolean, enumerated type, and so on, the variables defined by these types are real value type variables. The biggest difference between them is that the reference type variable is a pointer to an entity object, whereas a real value type variable is a real entity object. Since boxing and out of the box is a highly conceptual process, it will take a lot of space to introduce the details. This is beyond the main scope of this article. So this article only describes the operation related to the program that is out of the box. Specific steps, will be introduced in the next session of the combined procedure.

Three The idea of programming has been an important step in the solution:
(1). How to embed resources when the program is compiled. Resource files and programs are two separate files, to embed the resource file into the final generated program, you need to compile the "/resource" command, this command can be abbreviated as "/res". In this article, the name of the program 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 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 ("Images", assembly.getexecutingassembly ());

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

In the previous article, we learned that when creating a resource file, we used the AddResource () method to add resources, and the first parameter in his syntax was a string that the user could define, which is the unique identifier of the resource in the resource file, in the program design, This is the unique identifier used 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"));

Four Take a sample of a program to see how the resource file is used:
The resource file used by the following program example is the resource file "My.Resources" created in the previous article, with three lable components defined in the program, two of which are for displaying image resources in resource files, and another for displaying string resources in resource files. The icon for the program is taken from the icon resource in the resource file. The following is the source code for 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 a form
InitializeComponent ();
}
//Cleaner 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.getex Ecutingassembly ());
This.lblok = new Label ();
This.lblcancel = new Label ();
This.lblresource = new Label ();
this. SuspendLayout ();

This.lblOK.BackColor = System.Drawing.Color.White;
Using image resources in a 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);
Out of the box
Using image resources in a 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;
Out of the box
Using a string resource in a 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);
Out of the box
Using the icon resource in the resource file
Icon Icodemo = (icon) RM. GetObject ("Demo.ico"));
This. Icon = Icodemo;
This. Name = "Form1";
This. Text = "Use resource files in Visual C #!" " ;
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"));
}
}

Five Summarize:
So far we have completed all the programming in Visual C # resource files, the main content of which is the creation of resource files and the use of resource files two large, I would like to pass the introduction of these two articles, you should have a more comprehensive understanding of the resource file!




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.