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()); 
        }
    }

You May Also Like

About the Author: rnowik

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.