Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using embedded resources in C# console application

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.

like image 709
Pupper Avatar asked Jan 20 '12 04:01

Pupper


1 Answers

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

like image 141
Kerry H Avatar answered Sep 16 '22 14:09

Kerry H