How to embed the resource file in the .NET environment

xiaoxiao2021-03-06  46

main content:

How to embed resources in the .NET environment. It mainly tells how to embed, how to use two parts.

Namespaces and classes used:

System.Resources Namespace

System.Resources.ResourceWriter CLASSOURCEWRITER CLASS

System.Resources.ResourceManager CLASSOURMANAGER CLASSOUR

Tools used:

Visual Studio.net 2003

.NET Framework Ver1.1

The origin of the problem:

In general, one is included in a program, an icon, a string, and other resources. These resources are likely to be difficult to program because some reasons (such as environment variables) are difficult. The following method is how to turn our resources into a file, then embedded to the method of executable programs or assembly.

The first part makes a resource file

We first open Visual Studio.net to create a blank solution called Resource_assembly. Then we add a console project in this solution called ResourceWriter.

The following is added to the main.cs file:

Using system.drawing;

Using system.drawing.drawing2d;

Using system.drawing.image;

Using System.Resources;

Edit the code in the MIAN function:

[Stathread]

Static void

Main

(String [] ARGS)

{

Try

{

// Declare the ResourceWriter object, save the resource into myResources.dll file

System.Resources.ResourceWriter RW = New System.Resources.ResourceWriter ("MyResources.Resources");

// Load the picture

System.Console.writeline ("Reading Jpeg File ...");

System.drawing.image IMG = system.drawing.image.fromfile (@ "E: / my documents / resource_assembly / resourcewriter / resources / matt & andrs.jpg");

// Add a resource

System.Console.writeline ("Added a Image as a resource ...");

RW.AddResource ("MyPic", IMG);

// Declare string

String mystr = "I like Eric Claption, He is My Hero";

// Add a string resource

System.Console.writeline ("Adding a string ...");

Rw.addResource ("MyString", MyStr);

Rw.close ();

}

Catch

{

System.Console.writeline ("Here Is A Error, Writting Filed.");

Rw.close ();

System.Threading.Thread.Sleep (2000);

Return;

}

System.console.writeline ("Operation Complete.Press Enter to EXIT");

System.console.read ();

}

}

Among them, the RW object is an instance of system.resources.resourceWriter to write various resources into the resource file. The name of the resource file is MyResources.Resources. Then read a JPEG file and saved to myResources.Resources resource files. The path to the JPEG file is changed according to the actual situation. Then write a string in the MyResources.Resources resource file. There is an important method in the System.Resources.ResourceWriter class called AddResource. This method has two parameters, the first parameter is the resource name of this resource, which is used to extract resources later. The following parameters are references to the resource object to be added.

After this project is compiled, you will generate a resource file called MyResources.Resources in the current program files. This is what we want.

The second part will focus on the resource file embedding assembly and use

Let's add a new project resourceconsumer in the solution. This project is WinForm type. Join the resourceconsumer project to join us just created a good MyResources.Resources file. Right-click on myResources.Resources project, select Properties, select the drop-down box for generating an operation, select Embedded Resources. Only this compiler will only embed the resource file to the current executable or program set.

Next, how to call these resources. This step needs to be reflected. The getExecutingAnsembly method of the Assembly object is obtained by reflecting the instance reference to the current program itself. Then create an object RM of the ResourceManager type.

// Get a resource assembly by reflection

System.Reflection.Assembly asm = assembly.getexecutingassembly ();

// Initialize resource management object

RM = New ResourceManager ("ResourceconSumer.myResources", ASM);

Please pay attention to a problem when you create a ResourceManager object. In assembly internal resources are also named space, the general form is: application namespace. User-defined resource file name .resources. ResourceManager is automatically coupled with ".resources" according to the first parameter "ResourceConSumer.myResources" string given by the user, please do not deliberately add ".resources" or resourceManager will produce an exception. .

If you are not clear if you are in which namespace your resources is younger, you can use the tool ILDASM.EXE that comes with .NET Framework SDK to view the original data part of the program. You will clearly see your resource namespace.

Using resources inside the assembly, you can implement the getObject method in the ResourceManager class.

// extract strings from resources and display

THIS.PICTUREBOX1.Image = (image) RM.GetObject ("MyPic");

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

New Post(0)