Accessing Embedded Resources

on Friday, May 15, 2009

Accessing Embedded Resources using GetManifestResourceStream
How do you access resource files (bmp, mp3, etc) that are compiled into your Windows Forms executable? After googling the subject, the Assembly object's GetManifestResourceStream method seemed to be the solution. MSDN's entry on this subject makes it seem relatively simple. However, it took me over an hour to get it to work in my latest project. There are two things you must do in other to access an executable's resources:

1.You must know the exact name of resource (mynamespace.resource.resourcename).

2.You must embed the resource into your executable.

Accessing the resources means that you must have access to the executable's Assembly object.


Assembly resourceAssembly = Assembly.GetAssembly(typeof(Program));
string[] names=resourceAssembly.GetManifestResourceNames();
foreach (string str in names)
using (StreamReader stream = new StreamReader(resourceAssembly.GetManifestResourceStream("ConsoleApplication2.NewFolder1.Embed.xml")))
string str=stream.ReadToEnd();