Using embedded resources in C# console application
Asked Answered
N

1

11

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.

Nutriment answered 20/1, 2012 at 4:8 Comment(4)
Do you mean something like this or these possible duplicates?Monetmoneta
possible duplicate of How can I discover the "path" of an embedded resource?Sapor
Exactly what I was looking for. It's unfortunate that it was closed as too localized.Irmine
The SO mods ate too many bananas?! What's localized with this question?Wartburg
C
14

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

Cacogenics answered 20/1, 2012 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.