Click here to Skip to main content
11,431,483 members (65,356 online)
Click here to Skip to main content
Technical Blog

Embed resources in .NET assemblies

, 6 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to embed binary resources within .NET assembly and avoid file dependencies.

Motivation: To embed binary resources within .NET assembly and avoid file dependencies.

Problem: I was performing unit testing for my project and realized that I needed a file to be present in the same directory as the assembly. Now Visual Studio test project creates an out folder where it copies the binaries for execution. Initially I tried to include my binary file in the project and from the properties window set "Copy to Output Directory" to "Copy Always" hoping that after compilation file will be copied to the output folder and Visual Studio test framework will deploy it to the out folder. While the file was generated in the output folder (\bin\Output) it wasn't copied to the out folder at runtime (surprise surprise). So I thought of a broader solution to this fundamental problem. Answer: embed binaries in the assembly and extract them at runtime Smile | :) .

Steps:

  • Assume that embedded file is called myfile.bin. First step is to create a resource file in the project. This can be done from project properties -> Resources tab.
  • From resources window choose to add an existing file to the resources and select myfile.bin.
  • Use code below to extract the file at runtime at Assembly's execution path.

Code

private void ExtractFile()
{
    Assembly currentAssembly = Assembly.GetExecutingAssembly();
    string outputPath = Path.Combine(Path.GetDirectoryName(
           currentAssembly.Location), "myfile.bin");
    using (FileStream fStream = new FileStream(outputPath, FileMode.Create))
    {
        fStream.Write(Resources.myfile, 0, Resources.myfile.Length);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ashish Kaila
Software Developer (Senior) MixModes Inc. | Research In Motion
Canada Canada
Ashish worked for Microsoft for a number of years in Microsoft Visual Studio (Architect edition) and Windows Live division as a developer. Before that he was a developer consultant mainly involved in distributed service development / architecture. His main interests are distributed software architecture, patterns and practices and mobile device development.

Currently Ashish serves as a Technical Lead at RIM leading next generation BlackBerry media experience and also runs his own company MixModes Inc. specializing in .NET / WPF / Silverlight technologies. You can visit MixModes at http://mixmodes.com or follow it on Twitter @MixModes

In his free time he is an avid painter, hockey player and enjoys travelling. His blog is at: http://ashishkaila.serveblog.net
Follow on   Twitter

Comments and Discussions

 
QuestionWhat is Resources ? Pin
cangaran28-Nov-12 4:10
membercangaran28-Nov-12 4:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 6 Nov 2011
Article Copyright 2011 by Ashish Kaila
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid