Getting an embedded resource file out of an assembly

Source: Internet
Author: User
Getting an embedded resource file out of an assembly

Posted on Wednesday, August 03,200 5 By Bobby @ 4: 04 pm 23 comments I always embed supporting files in my applications and assemblies (images, scripts, XML docs, etc ). this makes it so you don't have to package these files alongside your application or assembly. this isn't as necessary when you are writing an application (as most apps already need an installer ), but it's extremely useful when developing server controls-which end up being assemblies and don't always warrant their own installers.

Embedding files within an assembly is pretty straightforward. Add the file to your project, go to properties and change the build action to "embedded resource". Done.

Super sweet right? So how the funk do I extract them out when I need to use them? With the code below foolio:

 /// <Summary> /// Extracts an embedded file out of a given assembly.  /// </Summary>  /// <Param name = "assemblyname"> The namespace of you assembly. </param>  /// <Param name = "FILENAME"> the name of the file to extract. </param>  /// <Returns> A stream containing the file data. </returns>  Public   Static Stream getembeddedfile ( String Assemblyname, String Filename ){ Try {System. reflection. Assembly= System. reflection. Assembly. Load (assemblyname); stream Str = A. getmanifestresourcestream (assemblyname +   "."   + Filename ); If (STR = Null ) Throw   New Exception ( "Cocould not locate embedded resource '"   + Filename +  "'In assemb '"   + Assemblyname +   "'" ); Return STR ;} Catch (Exception e ){ Throw   New Exception (assemblyname +   ":"   + E. Message );}}

A couple of notes about the embedding resources. if you're like me and you like to organize your resources, you Prolly have them grouped into folders. in that case, the filename parameter must include the path to the file. for example, if I have the following embedded Resource:

Project root/resources/images/top_left_corner.gif

The filename Arg wocould be:

Mynamespace.resources.images.top_left_corner.gif

Don't ask me why you use a period instead of a typical directory separator. Mine is not to question why.

Perhaps you don't know the name of the assembly, or are just too lazy to pass it along, here are some overloads that take in alternative types-type & assembly

Public StaticStream getembeddedfile (system. reflection. Assembly assembly,StringFilename ){StringAssemblyname=Assembly. getname (). Name;ReturnGetembeddedfile (assemblyname, filename );}Public StaticStream getembeddedfile (type,StringFilename ){StringAssemblyname=Type. Assembly. getname (). Name;ReturnGetembeddedfile (assemblyname, filename );}


So the following example returns a stream. But if you are constantly extracting XML docs, or bitmaps, wrap it up:

For bitmaps:

 
Public StaticBitmap getembeddedimage (type,StringFilename) {stream Str=Getembeddedfile (type, filename );Return NewBitmap (STR );}

For XML docs:

Public StaticXmldocument getembeddedxml (type,StringFilename) {stream Str=Getembeddedfile (type, filename); xmltextreader tr= NewXmltextreader (STR); xmldocument XML= NewXmldocument (); XML. Load (TR );ReturnXML ;}

Be careful when embedding a lot of files as it will significantly increase the size of your Assembly. Transferred from: http://www.csharper.net/blog/getting_an_embedded_resource_file_out_of_an_assembly.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.