5.1 Using resource files in command-line compilation ... 6
5.2 Using resource files in Visual Studio.NET ... 8
Vi. Concluding remarks ... 9
I. INTRODUCTION
Microsoft's. NET, published from the first edition of January 15, 2002 to today, has been widely used; from the time of the introduction of the domestic books of the very few, to today. NET related books are greatly enriched, there are more and more people-beginners or experienced programmers, in learning, application. NET. This article is oriented to. Net Some people who know, take C # as an example, introduce how to in. NET programming environment, you embed resource files (such as resource files that contain pictures, strings, and so on) into an assembly. The assembly described here can be an EXE file, but also a DLL file for other programs to call.
This article does not address the internationalization, localization, packaging, and deployment of resources, and interested readers can consult the. NET Framework SDK documentation.
Second, the SOFTWARE environment
Running the program in this article requires the following software environment: Windows 2000/xp,. NET Framework SDK. The code in this article runs through the following environments: Windows XP Professional,. NET Framework V1.1, Visual Studio.NET 2003.
III. Resource Documents
Almost every productive application needs to use resources. A resource is any non executable data that is logically deployed by an application. Resources can be displayed as error messages in the application or as part of the user interface. A resource can contain multiple forms of data, including strings, images, and persistent objects. By storing data in a resource file, you can change the data without recompiling the entire application.
In. NET, there are three resource files for both text files. resx files and. resources files. If the resource will contain only string data, then the text file is the simplest choice. If a resource will contain an object or a combination of strings and objects, you must create a. resx file or a. resources file. Note that only. resources files should be able to be embedded in common language runtime assemblies and satellite assemblies.
Iv. Creating resource Files
Create a resource file, there are two ways to write code and take advantage of software named ResEditor. The following is described in this part.
4.1 Write code to create a resource file
The. NET Framework Class Library provides a ResourceWriter class to create the. resources file. The ResourceWriter class is contained in the System.Resources namespace. The ResourceWriter class writes resources to the output file or output stream in the system default format.
Use the AddResource method in the ResourceWriter class to specify a resource as a name and value pair. Resource names are case-sensitive when used for lookups, but ResourceWriter will not allow the use of only case-only different. resources file names to make it easier to support authoring tools and help eliminate errors.
To create a resource file, create ResourceWriter with a unique filename, call AddResource at least once, call Generate to write the resource file to disk, and then call close to shut down the file.
The following example writes several strings to the myresources.resources file.
Example 1
This sample code comes from the. NET Framework SDK Documentation
Createrestest_1_1.cs
Using System;
Using System.Resources;
public class Writeresources {
public static void Main (string[] args) {
Creates a resource writer.
IResourceWriter writer = new ResourceWriter ("myresources.resources");
Compile code: CSC Createrestest_1_1.cs, after the compilation succeeds, generates an executable file named Createrestest_1_1.exe in the working directory; Generates a resource file named String.resources in the working directory.
This gives an example of a string written to a resource file, and the following example attempts to embed several pictures in 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");
Creates an image object from the specified file.
_bird.png, _butterfly.png files in current working directory
Where name is the resource name and value is the resource value
rw. AddResource ("Bird", _bird_pic);
rw. AddResource ("Butterfly", _butterfly_pic);
Writes to the file or stream, and closes it.
rw. Generate ();
rw. Close ();
}
}
Make sure the _bird.png and _butterfly.png files are in the current working directory. Compile code: CSC Createrestest_1_2.cs, if the compilation succeeds, generates Createrestest_1_2.exe, and runs the file, generates the resource file picture.resources.
4.2 Using the Resource Editor (ResEditor) to create a resource file
The. NET Framework contains a sample application called ResEditor that can help you create and edit resource files. ResEditor can create binary resource files (. resources) and XML resource files (. resX).
Generate ResEditor
The reseditor is provided in the form of source code with the. NET Framework SDK. ResEditor must be generated using the provided batch file before it can be used. Locate the \sdk\v1.1\samples\tutorials\resourcesandlocalization\reseditor folder, run the batch file BUILD.bat, and build the ResEditor.exe application after the compilation succeeds. In the author's environment, the path is as follows:
\program Files\Microsoft Visual Studio. NET 2003\sdk\v1.1\samples\tutorials\resourcesandlocalization\reseditor.
After you build ResEditor, you can use it to create and edit resource files.
Using ResEditor to create a resource file
Start the ResEditor application.
From the Add Drop-down menu, select the type of resource you want to add.
Type a name for 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 appropriate string in the box.
For images and other types of resources, browse to the appropriate file.
Repeat steps 3, 4, 5 for each resource that you want to add to the file.
From the File menu, click Save As to save the file. You can save the file as a. resources file, or you can save it as a. resX file.
Edit an existing resource file
You can use ResEditor to edit an existing resource file (. resources file and. ResX files). Use the following methods:
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 opens, and the resources it contains are 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 appropriate string in the 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 that 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, highlight it by clicking the resource, and then choose Delete from the Resources menu.
After you edit the resource file, select File, and then select Save As to save the file.
V. Use of resource documents
After you have created the resource files, it is easy to add them to your application. A binary resource file (. resources) or an XML resource file (. resX) can be added directly to your project. When you compile the project, the resource file is also compiled. You can retrieve an embedded resource (that is, a resource that has been compiled into an assembly) by using the ResourceManager class.
If you want to update resources in your program frequently without recompiling the entire solution, you can create a resource assembly.
5.1 Using resource files in command-line compilation
This example uses the resource file Picture.resources generated by the example 2 code. 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
Under the console, switch the working directory to the current code, the directory where the resource files reside, and run the CSharp compiler CSC (see the. NET Framework SDK for specific use). Ensure that the resource file exists and is correct, input: Csc/t:winexe/resource:picture.resources Userestest_2_1.cs, after the compilation succeeds obtains the UseResTest_2_1.cs.exe.
5.2 Using resource files in Visual Studio.NET
To add a resource file to your project: From the Project menu, select Add Existing Item. The Add Existing Item dialog box opens. Browse to the resource file you want to add to your project. It may be a. resources file, or it may be a. resX file. Select the appropriate file.
From the Build menu, select Build Solution to embed the resource file into your compiled project. Note that if you make a change to a resource file that has been added to your project, you need to select Rebuild Solution on the Build menu to make the changes take effect.
As for code, it's slightly different from Userestest_2_1.cs. Assuming that the namespace of the project is Project1, you need to put the Rescontainer in the constructor method:
ResourceManager RM
= new ResourceManager ("Picture", assembly.getexecutingassembly ());
To
ResourceManager rm = new ResourceManager ("Project1.picture",
Assembly.getexecutingassembly ());
If you do not add namespaces, you will not receive any error prompts when you build the solution, and errors will occur when compiling and executing. Strange time to finish ResourceManager RM
= new ResourceManager ("Picture", assembly.getexecutingassembly ()); In this line, RM is not NULL, and does not throw an exception, so it silently goes on. Execute to the next sentence:
The exception is thrown, and the exception information is as follows: "System.Resources.MissingManifestResourceException:Could not find No." Appropriate for the Specified culture (or the neutral culture) in the given assembly. Make sure \ "Picture.resources\" is correctly embedded or linked into assembly \ "Project1\" ".
The reason for this is that the IDE is a ghost, add the following resource file into its namespace. Try to delete the project's default namespace (right-click the project, Properties, General), and you don't need to add a namespace before the resource file.
Vi. concluding remarks
The creation and use of resource files is introduced here, where the text format and resource files in. resx format are not covered. One of the major uses of resource files-internationalization, and not within the scope of this article. In some programs, the resource file embedded in the assembly, can at least avoid the assembly file, the existence of the resources in the form of the dependency-if the Form.exe program needs to delete the picture file or not in the correct path, how to do? Of course, in the Visual Studio.NET View Designer, you can add resources such as pictures, but learn to do it yourself to create, manage, and use your resources without being spoiled by a good IDE such as Visual Studio.NET (it's hard not to be spoiled).
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.