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"); |
February 10th, 2008 on 9:01 am
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!
September 14th, 2008 on 2:15 am
Thank you very much, helped me figure out the same issue.
December 31st, 2008 on 8:09 am
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’)
January 3rd, 2010 on 2:05 am
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
February 22nd, 2010 on 8:19 pm
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”);