Create an instance explanation. The creation and use of resource files in net
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:
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.
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
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:
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.