Brian Hartsock's Blog

Assembly.GetManifestResourceStream()

by bhartsock on Nov.30, 2007, under .NET

Lately I have been hacking some things out with NHibernate. I ran across an interesting issue with calling 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");

Post to Twitter Post to Digg Post to Facebook Post to Reddit


5 Comments for this entry

  • 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!

  • Bicubic

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

  • 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’)

  • mahdi shahbazi

    Assembly assembly = this.GetType().Assembly;
    assembly.GetManifestResourceStream(
    assembly.GetName(),”nhibernate.cfg.xml”);

    this code will be work, ofcource you must set the property “build action” to “Embedded Resource” for selected file

  • Johann

    This code will NOT work — the assembly name is the WRONG prefix. It might work for you in the special case that your assembly name happens to be the same as your default namespace, but it will BREAK whenever you change that.

    Assembly assembly = this.GetType().Assembly;
    assembly.GetManifestResourceStream(
    assembly.GetName(),”nhibernate.cfg.xml”);

Leave a Reply