Ways to use resources in C #

Source: Internet
Author: User


Summarize a method of how to use resources in C #:
One: Using local files
1, the local to join the resources text (video, pictures, text or other) into the project, for example, we now add a up.bmp picture to the project, and placed under the folder resources,
2. Set the build action of up.bmp to "embedded Resource"
3. Read resources:
Assembly Assembly = assembly.getentryassembly ();
I do not know why, the above sometimes not, can write: Assembly Assembly = this. GetType (). Assembly;
System.IO.Stream Stream = assembly. GetManifestResourceStream ("OutlookBar.Resources.up.bmp");
OutlookBar represents a namespace, and resources represents a folder name;
If the check is placed directly in the root directory of the project, it is written as: assembly. GetManifestResourceStream ("OutlookBar.up.bmp");
Bitmap upimage = (Bitmap) image.fromstream (stream);


The second method: Create a new. resx file (testproject.resx) and join the Up.bmp file directly,
Read method: Bitmap upimage = testproject.up;


Third method: Create a separate resource file to store the Up.bmp file

1: Write to:
if (! File.exists ("Testproject.resources"))
File.create ("Testproject.resources"). Dispose ();

using (iresourcewriter RW = new ResourceWriter ("Testproject.resources"))
{
Image img = image.fromfile (@ "D:\up.bmp");
rw. AddResource ("Up.bmp", IMG);
rw. Close ();
}


2. Read the file
using (ResourceSet rs = new ResourceSet ("Testproject.resources"))
{
var image = (Bitmap) Rs. GetObject ("Down.bmp");
pictureBox1.Image = Image;
Rs. Close ();
}

NET resources are not limited to. resx files and can be used in any storage format
: See http://www.cnblogs.com/artech/archive/2010/12/16/Reource_framewok_01.html for details

Ways to use resources in C #

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.