C # Creating and using resource files

Source: Internet
Author: User

Create a resource file

A resource file is a file that stores resources as its name implies. Resource files in the program design has its own unique advantages, independent of the source program, so that the resource files can be used by multiple programs. At the same time in the program design, sometimes for security or other factors of consideration, the important things stored in the resource file, can also achieve a confidential, safe effect. So what exactly is in the resource file used by Visual C #? Creating a resource file with Visual C # can roughly hold three types of data resources, namely byte arrays, various objects, and strings. This article will combine a program example to illustrate how you can create a resource file with Visual C #.

A Some concepts and theories that are used in Visual C # to create resource files:
A name in the. Net FrameWork SDK, called the System.Resources namespace, provides the application with many classes and interfaces for creating, storing, and using resource files in this namespace. One of the classes called Resourcewriter,visual C # is to implement the creation and storage of resource files by invoking this class.

Two How Visual C # creates a resource file:
To inherit a ResourceWriter class first, and then call a method generate () of the ResourceWriter class, you can generate a resource file. The specific statements are as follows:

ResourceWriter rw = new ResourceWriter ("My.Resources");
rw. Generate ();

At this point in the disk will produce a name called "My.Resources" resource file, but at this time the resource file does not have any content, let's see 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 join different resources in Visual C #.

(1). Add a byte array with the syntax format:

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 in the syntax format:

public void AddResource (String, object);

Note: Where string is the unique identifier of this object in the program when using a resource file

In this procedure, we use this method of invocation 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 an icon to the resource file
The following is the inclusion of an image 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 a string with the following syntax:

public void AddResource (string1, string2);

Note: Where string1 is used in a resource file, the unique identifier of this string in the program is so 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, it is important to note, after that, that you save the resource file and close the resource file as follows:

rw. Close ();

Four To create the source code for the resource file:
Through the above discussion, we are not difficult to understand the following code. The function of the following program code is to create a resource file named "My.Resources" and include an icon resource, several image resources, and a string resource in this 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 better to remind that after the successful compilation of the file, in the execution of this file, you must be sure to execute the same directory in the file, there is the above code mentioned in the name of the icon and image, otherwise in the creation of the resource file will be error.

Five Summarize:
It is not a complex process to create a resource file with Visual C #, and in the next article we will show you how to use resources in a resource file in Visual C #. This is the focus and difficulty of programming Visual C # resource files. Of course, the resource file used in this article is the resource file created in this document.

working with resource files

You have described how to create a resource file in Visual C # in Visual C # resource file programming-Create a resource file. This article will go on to the topic of the previous article to explore another issue 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, several picture resources, and a string resource, with the name "MyStr" in the file. We will use this resource file created in the previous article as an object, with a specific program example to see how the resource file is used in Visual C #.
A The software environment for the design and operation of this paper:
(1). Microsoft 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). and the mutual conversion between the two, in Visual C # is called boxing (Boxing) and Out of the box (unboxing). Boxing is the process of converting a real-valued type variable into a reference-type variable, which is the case. So what type of variable is a variable of reference type, and what type of variable is a real value type variable? What is the difference between the two? Reference types in Visual C # refer 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 common use of integers, booleans, enumerations, etc., 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 the entity object, and the real value type variable is the actual entity object. Because boxing and out of the box is a very conceptual operation process, detailed introduction needs a lot of space. And this is beyond the main scope of this paper. So this article only describes the program-related operations that are 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 way of solving:
(1). How to embed resources in a program compile time. Resource files and programs are two separate files, to embed the resource files in the final generated program, you need to add the "/resource" command at compile time, this command can be abbreviated as "/res". In this article the name of the program is "Use.cs", the name of the resource file is "My.Resources", then the compiler command to embed the resource into the 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 about the creation and use of resource files. There is a class for ResourceManager in this namespace, and the primary function of this class is to manage and use resource files. Visual C # is a resource in this class that manages and uses resources in a resource file in an embedded 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 a resource file in a program:

In the previous article, we learned that when creating a resource file, the AddResource () method was used to add resources, and the first parameter in his syntax was a user-definable string, which was the unique identifier of the resource in the resource file, and in the program design, is to use a resource with this unique identifier. So how do you get the resources you need with this identifier in your program? This will use the GetObject () and GetString () methods in the ResourceManager class. These two methods function to obtain the specified resource. Here are the syntax for these two methods:

Object Getsting (String)
Object GetObject (String)

where "String" is the unique identifier of the resource in the resource file. The attentive reader may have noticed that the return value of these two methods is a variable of type object, which is a variable of reference type, and the string or image in the program is a real value type variable. This requires a conversion, which is the above-mentioned boxing and unpacking. The following code extracts the string, image, and icon resources from the resource file:

To extract a string resource:

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"));

Four In conjunction with a program example, consider the specific use of resource files:
The resource file used in the following program example is the resource file "My.Resources" created in the previous article, and the program defines three lable components, of which two is the function of displaying the image resource in the resource file, and the other is to display the string resource in the resource file. The icon for the program is taken from the icon resource in the resource file. Here 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 ()
{
Initializing components in a form
InitializeComponent ();
}
Purging resources used in a 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;
Using the 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 the 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
Use 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 box
//Use 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 finished programming in Visual C # resource files, the main content is the creation of resource files and use of resource files two, I would like to pass the introduction of these two articles, you should have a more comprehensive understanding of resource files!

C # Creating and using 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.