How can I discover the "path" of an embedded resource?


I am storing a PNG as an embedded resource in an assembly. From within the same assembly I have some code like this:

Bitmap image = new Bitmap(typeof(MyClass), "Resources.file.png");

The file, named "file.png" is stored in the "Resources" folder (within Visual Studio), and is marked as an embedded resource.

The code fails with an exception saying:

Resource MyNamespace.Resources.file.png cannot be found in class MyNamespace.MyClass

I have identical code (in a different assembly, loading a different resource) which works. So I know the technique is sound. My problem is I end up spending a lot of time trying to figure out what the correct path is. If I could simply query (eg. in the debugger) the assembly to find the correct path, that would save me a load of headaches.

9/5/2014 7:07:31 AM

Accepted Answer

This will get you a string array of all the resources:

9/15/2010 7:16:41 AM

I find myself forgetting how to do this every time as well so I just wrap the two one-liners that I need in a little class:

public class Utility
    /// <summary>
    /// Takes the full name of a resource and loads it in to a stream.
    /// </summary>
    /// <param name="resourceName">Assuming an embedded resource is a file
    /// called info.png and is located in a folder called Resources, it
    /// will be compiled in to the assembly with this fully qualified
    /// name: That is the string
    /// that you should pass to this method.</param>
    /// <returns></returns>
    public static Stream GetEmbeddedResourceStream(string resourceName)
        return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

    /// <summary>
    /// Get the list of all emdedded resources in the assembly.
    /// </summary>
    /// <returns>An array of fully qualified resource names</returns>
    public static string[] GetEmbeddedResourceNames()
        return Assembly.GetExecutingAssembly().GetManifestResourceNames();

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow