C#: Using An Embedded Resource

Quicknote: To embed resources in an assembly you need to add the resource to the project and set the resources build action property to “Embedded Resource”. I normally add resources to a seperate folder named myresources (hence the myresources part of the manifestResource string in the example below). To access the resource use the following code (note the manifest resource string is case sensitive and is of the form namespace.folder.resourcename):

using System;
using System.Reflection;
using System.IO;
namespace embeddedresourcestest
{
    class Program
    {
        static void Main(string[] args)
        {
            string manifestResource = "embeddedresourcestest.myresources.myresource.txt"; 
            Assembly _assembly = Assembly.GetExecutingAssembly();
            Stream myStream = _assembly.GetManifestResourceStream(manifestResource);
            StreamReader textStreamReader = new StreamReader(myStream);
            Console.Write(textStreamReader.ReadToEnd()); 
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>