Embedding a resource into a .NET assembly and loading it

Sometimes our program needs resources in the form of files. An example is icons to show on buttons. Another example is the text of GNU GPL for display on a certain dialog box. Kanji dictionaries might use the text-based kanjidic file.

The obvious approach is to put the files on the file system. The advantage is that the resources can be updated or replaced easily. The disadvantage is file system clutter and external dependency (remove the resources and the program won’t run).

The other solution is to embed the resources in the executable or library itself. In this way, the executable/library will be self-contained.

Here’s how to do it in .NET using C#.

For the purpose of this guide, suppose the file to embed is test.txt which contains:

Line 1 of test.txt
Line 2 of test.txt

To embed the file into an assembly, just add the switch /res:FILENAME when compiling. For example, if the source code is Embed.cs then the command to compile should be:

csc /res:test.txt Embed.cs

(Replace csc with mcs if Mono’s compiler is used)

If the file is disassembled using Mono’s monodis, this will be found:

.mresource public 'test.txt'
{
}

which indicates that the file test.txt is indeed embedded into the assembly.

An example of a program which loads the resource is:

using System;
using System.IO;
using System.Reflection;

class Embed
{
  static void Main()
  {
    Assembly a = Assembly.GetAssembly(typeof(Embed));
    using(Stream s = a.GetManifestResourceStream("test.txt"))
    {
      using(StreamReader sr = new StreamReader(s))
      {
        Console.WriteLine(sr.ReadToEnd());
      }
    }
  }
}

The class from the System namespace that is used is Console. From System.IO is Stream and StreamReader while from System.Reflection is Assembly.

First, load the assembly that contains the resouce. Because the only class in the assembly is Embed, it is used to get the assembly:

Assembly a = Assembly.GetAssembly(typeof(Embed));

Now is the real meat. To open the stream to a resource in the assembly, call GetManifestResourceStream:

using(Stream s = a.GetManifestResourceStream("test.txt"))

The Stream object returned is just like a Stream object when a file is loaded from the file system. After that it’s just normal I/O.

If for some reason the embedded filename must differ from the original filename, use the option:

/res:FILENAME,EMBEDDED_FILENAME
Share and Enjoy:
  • bodytext
  • del.icio.us
  • Technorati
  • Slashdot
  • StumbleUpon
  • Sphinn
  • Facebook
  • Mixx
  • Google
  • TwitThis
  • Live

2 Responses to “Embedding a resource into a .NET assembly and loading it”

  1. charlie root Says:

    Great !
    thanx

  2. Embedding a resource into a .NET assembly and loading it « Singularity on the Plane Says:

    [...] a resource into a .NET assembly and loading it This post has been moved to singularity.agronesia.net: “Embedding a resource into a .NET assembly and loading it”. Please visit the new [...]

Leave a Reply