Delen via


Loading embedded resources in C# using GetManifestResourceStream

Did you ever add some resource (some xml, bmp, mp3, etc) to your project yet were not able to load them? I ran into this problem when adding an xml file, which contained some settings that are included when the code is built. Using the following code snippet, I tried to load the file but it wasn't found:

    System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("ActivityListItemData.xml");

It turns out the resource name was not correctly decorated. I examined all the available resources using the following line:

    string[] names = this.GetType().Assembly.GetManifestResourceNames();

and found instead that the resource name was actually "Alexdan.Properties.ActivityListItemData.xml"

With that change, I was able to load the resource. Here's the final code:

    System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("Alexdan.Properties.ActivityListItemData.xml"); 

See https://www.attilan.com/2006/08/accessing-embedded-resources-using.html for a really great writeup about loading and accessing resources.

Comments