Embed resource files in .NET to the program

xiaoxiao2021-03-06  66

Write: Jim XU date: October 6, 2004 Keywords: .NET, C #, resource file (.resources file)

First, the introduction ... 3

Second, the software environment ... 3

Third, resource files ... 3

Fourth, create a resource file ... 3

V. Use resource files ... 6

5.1 Use resource files in the command line ... 6

5.2 Use resource files in Visual Studio.net ... 8

Sixth, conclude ... 9

First, the introduction

Microsoft's .NET from

year 2002

January

15th

The first version released to this today, has been widely used; from the time of the domestic related books, it is greatly rich today .NET's related books, there are more and more people - beginners or experience Programmer, learning, applying .NET. In this paper, it is aware of .NET, with C # Taking C # Takes how to embed the resource file (such as a resource file containing the image, string, etc.) in the .NET programming environment. The set of assemblies here can be an Exe file, or a DLL file called by other programs.

This article does not involve internationalization, localization, packaging, and deployment resources, interested readers can consult the .NET Framework SDK documentation.

Second, the software environment

Running this article requires the following software environment: Windows 2000 / XP, .NET Framework SDK. The code in this article runs through: Windows XP Professional, .NET Framework V1.1, Visual Studio.net 2003.

Third, resource file

Almost every productive application requires resources. Resources are logically deployed any non-executable data. Resources can be displayed as an error message in the application, or as part of the user interface. Resources can include multiple forms of data, including strings, images, and persistent objects. By storing data in the resource file, you can change the data without recompilating the entire application.

In .NET, there are text files .resx files, and .resources files three resource files. If the resource contains only string data, the text file is the simplest selection. If the resource contains a combination of objects or strings and objects, you must create a .resx file or .resources file. Note that only .resources files should be embedded in the public language runtime assembly and the accessory set.

Fourth, create a resource file

Create a resource file, have two ways to write code and using software named ReSeditor. The following is described below.

4.1 Write code to create a resource file

The ResourceWriter class is provided in the .NET Framework class library to create a .resources file. The ResourceWriter class is included in the System.Resources namespace. The ResourceWriter class writes the resource to the output file or output stream in the system default format.

Use the AddResource to use the AddResource method to specify the resource as name and value pair in the ResourceWriter class. The resource name is case sensitive when it is used for finding, but to easier support for creative tools and help eliminating errors, ResourceWrit will not allow for use only cases different .Resources file names.

To create a resource file, create a resourceWriter with the unique file name, then call at least once, then call the generate to write the resource file into the disk, and then call the Close to close the file.

The following example writes several strings to myResources.Resources files.

//example 1

// This example code is from the .NET Framework SDK documentation

//Createrestest_1_1.cs

Using system;

Using System.Resources;

Public class whp {

Public static void

Main

(String [] args) {

// Creates a Resource Writer.

IResourceWriter Writer = New ResourceWriter ("MyResources.Resources");

// Adds resources to the resource write.

Writer.AddResource ("String 1", "first string");

Writer.AddResource ("String 2", "SECOND STRING");

Writer.AddResource ("String 3", "Third String");

// Writes the resources to the file or stream, and closes it.

Writer.close ();

}

}

Compile code: CSC CreateresTest_1_1.cs, after the compilation is successful, the executable file named CreateresTest_1_1.exe is born in the working directory; running the file, generated in the working directory for resource files for String.Resources.

The above examples give the string write resource files. The following example attempts to embed several images into the resource file MyResources.Resources.

// Example 2

//Createrestest_1_2.cs

Using system;

Using system.drawing;

Using System.Resources;

Public Class CreatPicResource

{

Public static void

Main

()

{

// Creates a Resource Writer.

ResourceWriter RW = New ResourceWriter ("Picture.Resources");

// Create an Image object from the specified file.

//_Bird.png, _butterfly.png file in the current work directory

Image _bird_pic = image.fromfile ("_bird.png");

Image _butterfly_pic = image.fromfile ("_butterfly.png");

// Add image object to the resource file

//ResourceWritername.AddResource (String name, Object value);

// where Name is the resource name, value is the resource value

rw.addResource ("Bird", _bird_pic;

RW.AddResource ("Butterfly", _Butterfly_PIC);

// Writes the resources to the file or stream, and closes it.

Rw.generate ();

Rw.close ();

}

}

Make sure _bird.png and _butterfly.png files are in the current work directory. Compile Code: CSC Createrestest_1_2.cs, such as compilation success, generate createrestest_1_2.exe; run the file, generate resource files Picture.Resources.

4.2 Using the Resource Editor to create a resource file

The .NET Framework contains a sample application called ReSeditor, which helps you create and edit the resource file. Reseditor can create binary resource files (.resources) and XML resource files (.resx). Generate reSeditor

ReSeditor is provided with the .NET Framework SDK in the form of the source code. You must use the supplied batch file to generate a reSeditor before you can use it. Find the /sdk/v1.1/samples/tutorials/resources/resser folder, run the batch file build.bat, after the compilation is successful, generate the reSeditor.exe application. In the environment used by the author, the path is as follows:

/ Program Files / Microsoft Visual Studio .NET 2003 / SDK / V1.1 / Samples / Tutorials / ResourcesAndlocalization / Reseditor.

After generating reSeditor, you can use it to create, edit the resource file.

Create a resource file using ReSeditor

Start the reSeditor application.

Select the type of resource you want to add from the Add drop-down menu.

Type the name of the resource in the Add text box, and then click the Add button to add the resource item to the file.

In the main pane, click the cell next to the resource name to specify a value.

For the "string" resource, type the corresponding string in this box.

For "images" and other types of resources, browse to the appropriate file.

Repeat steps 3, 4, 5 for each resource to be added to the file.

In the File menu, click Save As to save the file. You can save your file as .resources file, you can save it as .resx files.

Edit existing resource files

You can edit existing resource files (.resources files and .resx files) using ReSeditor. The method is as follows:

Start the reSeditor application.

On the File menu, click Open.

Browse to the appropriate resource file in the Open Resource File dialog box.

The resource file is opened, and the resource it contains is displayed in the main pane.

If you want to change the value of any resource, click the cell next to the resource name and specify the correct value.

For the "string" resource, type the corresponding string in this box.

For "images" and other types of resources, browse to the appropriate file.

If you want to rename a resource, do the following:

Highlight it by clicking the resource you want to rename.

Type a new name in the "Rename" text box.

Click the "Rename" button to apply the new name.

If you want to delete a resource, you have highlighted it by clicking this resource, then select Delete from the Resource menu.

After editing the resource file, select File, then select Save As to save the file.

V. Use resource files

Once you have created a resource file, it is easy to add them to your application. Binary resource files (.resources) or XML resource files (.resx) can be added directly to your project. When compiling the project, the resource file is also compiled. You can retrieve embedded resources by using the ResourceManager class (that is, the resources you have compiled into the program set).

If you want to update the resources in the program, you can create a resource assembly without recompiling the entire solution.

5.1 Use the resource file in the command line

Here, the resource files generated by Example 2 is used as an example. The sample code createrestest_2_1.cs is as follows:

// Example 3

//Createrestest_2_1.cs

Using system;

Using system.drawing;

Using system.windows.forms;

Using System.Resources;

Using system.reflection;

Public Class Testresform: System.Windows.Forms.form

{

Private PictureBox PicBox1;

Private PictureBox Picbox2;

Public Testresform ()

{

PicBox1 = new picturebox ();

PicBox1.location = new point (0,0);

PicBox1.width = Rescontainer.instance.ButterflyImage.width;

PicBox1.height = Rescontainer.instance.ButterflyImage.Height;

PicBox1.Image = Rescontainer.instance.ButterflyImage;

PicBox2 = new picturebox ();

PicBox2.Location = New Point (0,100);

PicBox2.width = Rescontainer.instance.birdimage.width;

PicBox2.height = Rescontainer.instance.birdimage.height;

PicBox2.image = rescontainer.instance.birdimage;

Controls.add (PicBox1);

Controls.add (picbox2);

this.size = new size (200,200);

}

Public static void

Main

()

{

Application.run (New Testresform ());

}

}

Public Class Rescontainer

{

// Data MEMBERS

Private image _birdimage = null;

Private image_ButterflyImage = NULL;

Private static rescontainer _INSTANCE = new rescontainer ();

// conncture

Private rescontainer ()

{

Try

{

ResourceManager RM = New ResourceManager ("Picture",

AskEMBLY.GETEXECUTINGASSEMBLY ());

_ButterflyImage = (image) (RM.GetObject ("Butterfly");

_Birdimage = (image) (RM.GetObject ("BIRD"));

}

Catch (Exception EX)

{

EX.TOSTRING ();

}

}

// Properties

Public Static Rescontainer Instance

{

Get {

Return_INSTANCE;

}

}

Public Image ButterflyImage

{

Get {

Return_ButterflyImage;

}

}

Public Image Birdimage

{

Get {

Return_Birdimage;

}

}

}

Under the console, switch the work directory to the current code, the resource file is located, run the CSHARP compiler CSC (see .NET Framework SDK). Make sure that the resource file exists and correct, enter: CSC / T: Winexe /Resource: Picture.Resources Userestest_2_1.cs, you get Userestest_2_1.cs.exe after compiling. 5.2 Using resource files in Visual Studio.net

Add a resource file to your project: From the Projects menu, choose Add Existing Item. The Add Existing Item dialog opens. Browse to the resource file to be added to the project. It may be .resources file, or it may be .resx file. Select the appropriate file.

In the Generation menu, select Generate Solution to embed the resource file into your compiled project. Note that if you make changes to the resource files added to the project, you need to select "Regenerate Solution" in the Generation menu to make changes to change.

As for the code, it is slightly different from the userestest_2_1.cs. Assuming that the namespace of the project is Project1, you need to put the Rescontainer constructor:

ResourceManager RM

= New ResourceManager ("Picture", Assembly.GetexecutingAssembly ());

Change to:

ResourceManager RM = New ResourceManager ("Project1.Picture",

AskEMBLY.GETEXECUTINGASSEMBLY ());

If you don't add namespace, you don't have any error prompts when you generate a solution, and you will have an error in compilation. When it is strange, it is executed for ResourceManager RM.

= New ResourceManager ("Picture", Assembly.GetexecutingAssembly (); this line is not null, nor throwing an exception, so quietly. Performed to the next sentence:

_ButterflyImage = (image) (RM.GetObject ("Butterfly");

An exception is thrown, the exception information is as follows: "System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture (or the neutral culture) in the given assembly Make sure /"picture.resources/." Was correctly embedded or linked INTO Assembly / "Project1 /" ".

The reason should be the ghost of the IDE, and the added such as resource files is also included in its namespace. Try the default namespace of the project (right-click project, attribute, general), you don't need to add a namespace before the resource file.

Sixth, conclude

转载请注明原文地址:https://www.9cbs.com/read-89676.html

New Post(0)