How to use embedded resources in C #

Source: Internet
Author: User

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

 

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.