C # How to use embedded resources (text and images)
Method 1: directly use a local file
1. Add images and files to a project (you can create a separate directory to store them, such as resources), for example:
Progress.gifand textfile.txt
2. Set images and files to embedded resources.
3. Use getmanifestresourcestream () to read resources:
System. reflection. Assembly ASM = system. reflection. Assembly. getentryassembly ();
System. Io. Stream imgstream = ASM. getmanifestresourcestream ("mynamespace.resources.progress.gif ");
Picturebox1.image = system. Drawing. image. fromstream (Stream );
System. Io. streamreader txtstream = new system. Io. streamreader (_ assembly. getmanifestresourcestream ("mynamespace.resources.textfile.txt "));
Textbox1.text = txtstream. Readline ();
Note: If the resource is stored in a separate resouces directory, the parameter of the getmanifestresourcestream () method is "mynamespace.resources.progress.gif". If the resource is directly in the project root directory, it is "mynamespace.progress.gif ". That is, the parameter naming rule is: Project namespace. Resource subdirectory name. Resource subdirectory name ........ File Name. extension. The resource sub-directory can have multiple layers or no.
References: http://support.microsoft.com/kb/319292/zh-tw
Method 2: Compile resources independently
1. Execute the following code in any project:
System. Resources. resourcewriter RW = new system. Resources. resourcewriter ("test. Resources ");
Image IMG = image. fromfile ("progress.gif ");
RW. addresource ("progress", IMG); // picture
RW. addresource ("loading", "loading, please wait..."); // text
RW. Close ();
Note: The file extension must be resources.
2. Add test. resources to the project where resources are to be used, and set the generated operation to embedded resources.
3. Read the resource using the ResourceManager () method.
Assembly ASM = assembly. getentryassembly ();
ResourceManager Rm = new ResourceManager ("mynamespace. test"); // mynamespace is the namespace of your program, and test is test. Resources.
Picturebox1.image = (image) Rm. GetObject ("progress"); // read the image
Textbox1.text = RM. getstring ("loading"); // read text
Note: The parameter naming rules of the ResourceManager () method are as follows: Project namespace. Resource subdirectory name. Resource subdirectory name ........ Resource file name. Note that there is no extension.
References: http://blog.csdn.net/lne818/archive/2008/02/27/2125143.aspx
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yuvmen/archive/2009/08/22/4473514.aspx