Using embedded resources in C# console application

Pupper picture Pupper · Jan 20, 2012 · Viewed 12.7k times · Source

I'm trying to embed an XML file into a C# console application via Right clicking on file -> Build Action -> Embedded Resource.

How do I then access this embedded resource?

XDocument XMLDoc = XDocument.Load(???);

Edit: Hi all, despite all the bashing this question received, here's an update.

I managed to get it working by using

XDocument.Load(new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Namespace.FolderName.FileName.Extension")))

It didn't work previously because the folder name containing the resource file within the project was not included (none of the examples I found seemed to have that).

Thanks everyone who tried to help.

Answer

Kerry H picture Kerry H · Jan 20, 2012

Something along these lines

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var stream =  Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApplication1.XMLFile1.xml");
            StreamReader reader = new StreamReader(stream);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reader.ReadToEnd());
        }
    }
}

Here is a link to the Microsoft doc that describes how to do it. http://support.microsoft.com/kb/319292