Search code examples
c#embedded-resource

I can I access an Embedded Resource and save it as a file?


I have an XML file that I want to include with my application as an Embedded Resource, so that the file is compiled in with the .exe and is unavailable to the user.

Right now, the file is located at

[approot]/ConfigurationFiles/Defaults/Core.xml

I'd like to be able to create an XDocument from this embedded resource and then save that to the disk so the user can access it.

How can I do this? How can I access the embedded resource and create an XDocument from it?


Solution

  • You just open a stream, read the resource and do with it what you want:

    using (Stream stream = assembly.GetManifestResourceStream(".../ConfigurationFiles/Defaults/Core.xml"));
    {
      // turn it to a XDocument and store it
      XDocument doc = XDocument.Load(stream);
      // ...
    }  
    

    The path name to the resource is composed by:

    <Assembly default namespace>.<path to the resource>.<resource name>