C#: How to use assembly embedded resources

After some digging around, I found that it actually wasn’t that difficult at all.

Getting it in

Putting something in an assembly as an embedded resource is pretty easy. At least if you are using Visual Studio. Just add the file to your project, click on it, and then under Properties set Build Action to Embedded Resource. And thats it!

Getting it out

Lets say we want an image called hello.png in a folder called Wopdidoo as a Stream.

If you are not executing code in the same assembly you just have to get that assembly reference in a different way. I often use Assembly.GetAssembly(typeof(T)), where T is some type you know exists in the same assembly as the file you want. The rest is the same.

As far as I know, you use the stream as any other stream. Not sure if it is writable though? Probably not… let me know if you have some brilliant clues on that matter

❓ Remember to Dispose it when you are done.

Finding it

I sometimes find it a bit difficult to figure out that string which identifies the resource. I then often use the following code to “find” it:

Post navigation

Greetings! I am Torleif Berger, currently working as a Software/Web Developer. I'm a Seventh-Day Adventist, an introvert and a highly sensitive person (HSP). I like to dabble in various types of juggling, in-door rowing and other activities, but most of my time is spent in front of a screen. Have a look around c'',)