Turn http://www.jb51.net/article/84660.htm
This article describes the use of GetManifestResourceStream to read embedded resources, and to use. RESX resource files Embedded resources, we hope to help.
There are two ways to embed a resource (bitmap, icon, or cursor) in dotnet, and one is to add the resource file directly to the project as an embedded resource and get the stream of the resource in code through the assembly GetManifestResourceStream method. Another approach is to include in the project. A RESX resource file that adds resources to a resource file that is managed by the ResourceManager class to manage its resources uniformly.
These two methods are detailed below
First, using GetManifestResourceStream to read embedded resources
1. Join the resource file
Add the resource file that you want to embed into your project directly, and you can add it to the project's root directory, which you can add to any directory in your project.
2. Set the "BuildAction" property of the resource file
Set the BuildAction property of the embedded resource file to "Embedded Resource"
3. Using embedded resources in your code
//get the namespace where the class is runningType Type=Methodbase.getcurrentmethod (). DeclaringType; string_namespace =type. Namespace; //get the current running assemblyAssembly _assembly=assembly.getexecutingassembly ();//generate resource names based on namespace and file name stringResourceName = _namespace +". Directory. Bitmapmanifest.bmp"; //gets the stream of this resource from assembly based on the resource nameStream Stream=_assembly. GetManifestResourceStream (resourcename); Image MyImage= Image.fromstream (stream);
Note the composition rules for resource names:
The project default namespace. The resource is in the directory where the project resides. resource File name
The bitmapmanifest.bmp bitmap in the code above is in the directory directory of the project. The project default namespace if the resource file is directly in the project root directory. resource file name
Give an example of an XML resource:
//get the namespace where the class is runningType Type=Methodbase.getcurrentmethod (). DeclaringType; string_namespace =type. Namespace; //get the current running assemblyAssembly _assembly=assembly.getexecutingassembly ();//generate resource names based on namespace and file name stringResourceName = _namespace +". Xmlfiletest.xml"; //gets the stream of this resource from assembly based on the resource nameStream Stream=_assembly. GetManifestResourceStream (resourcename); XmlDocument xmldoc=NULL; xmldoc=NewXmlDocument (); Xmldoc.load (stream);
Second, use. ResX resource file embedding resources
1. New Resource file
Creates a new resource file in the project, the resource file is suffixed with a. resx, and a new Designer.cs file with the same name as the resource file is created.
In fact, the greatest use of resource files is to use a multi-language version of the software to save different language resources, such as menu text in different languages, you can put different language strings under the same resource type of different resource packages, When the program runs, different packages are displayed according to the culture of the runtime system, displaying strings in different languages.
After you create a new resource file, you can add a resource file to the resource file:
Figure 1. Resource types that can be embedded in a RESX resource file
Resources can add resources such as strings, bitmaps, icons, audio, files, and so on.
The added resources will be saved in the project's Resource folder.
2. Set the "BuildAction" property of the resource file
The "BuildAction" property of all resource files in the Resources folder is set to "Embedded Resource".
3. Resource existence mode
. resx resource file management resources can be in two forms, one in the resources folder as a generic file, and the other in a. resx resource file after Base64 encoding.
Figure 2 How resources exist in a RESX resource file
Open the. resx resource file, select the resource, and the persistence attribute in the attribute determines the existence of the resource. Two forms of existence of a resource, which are called in the same way in code.
4. Using embedded resources in your code
//get the namespace where the class is runningType Type=Methodbase.getcurrentmethod (). DeclaringType; string_namespace =type. Namespace; //gets the current main assemblyAssembly currentassembly=assembly.getexecutingassembly ();//the ROOT name of the resource stringResourcerootname = _namespace +". ResourceTest"; //instantiating resource management classesResourceManager ResourceManager=NewResourceManager (Resourcerootname, currentassembly);//get resource objects by resource nameIcon Myicon= (Icon) resourcemanager.getobject ("icontest");
Note the rule for the root name of the resource:
The ROOT name of the resource is the full name of the resource file class.
Open the appropriate Designer.cs file for the. resx resource file to see the full name of the resource class: namespace. Resource class name.
The resource name is the name of a resource in the. resx resource file.
Open the. resx resource file to see the names of the individual resources. The name of the icon file Icontest.ico in the image above is icontest.
To retrieve the string resource, call the GetString method.
To retrieve other types of resources, call the GetObject method and explicitly convert the resulting resource to the corresponding type.
5. Multi-lingual Resource application
// get the current language environment CultureInfo ci = Thread.CurrentThread.CurrentCulture; // CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture; = (Icon) resourcemanager.getobject ("IconText", CI);
How to read embedded and use resource files in. net