Assembly.GetManifestResourceStream()

Lately I have been hacking some things out with NHibernate. I ran across an interesting issue with call Assembly.GetManifestResourceStream(). For NHibernate, I have different session factory configuration files for each entity assembly. So I needed to load an embedded resource from an assembly and pass it to NHibernate.

This was my first attempt…

1
2
Assembly assembly = this.GetType().Assembly;
assembly.GetManifestResourceStream("nhibernate.cfg.xml");

…which didn’t work.

After some research, I found this article. Basically, you can’t just specify the resource name. You have to specify the entire assembly name before the resource name. Thanks .NET for another wonderfully documented feature that makes no sense.

1
2
3
Assembly assembly = this.GetType().Assembly;
assembly.GetManifestResourceStream(
    assembly.GetName().Name + ".nhibernate.cfg.xml");

3 Responses to “Assembly.GetManifestResourceStream()”


  1. 1 James

    Actually, I think this it should be DefaultNamespace.PathToResourceFile, not the assembly name. And this is working because by default your default namespace IS the assembly name, but not in my case :(. But this post did help me figure out what .NET was expecting, so thanks!

  2. 2 Bicubic

    Thank you very much, helped me figure out the same issue.

  3. 3 Moth

    Thanks, it also helped me figuring out what was wrong in my .netcf application while trying to load embedded wav files.
    I learned two things:
    1) Use the .NET Reflector tool to actually figure out the path to the resource.
    2) In VS2005, the build action for the actual embedded resource (in the properties window) must be set to ‘Embedded Resource’ instead of the default action (which is ‘None’)

Leave a Reply