Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetManifestResourceStream returns NULL

You can check that the resources are correctly embedded by using

//From the assembly where this code lives!
this.GetType().Assembly.GetManifestResourceNames()

//or from the entry point to the application - there is a difference!
Assembly.GetExecutingAssembly().GetManifestResourceNames()

when debugging. This will list all the (fully qualified) names of all resources embedded in the assembly your code is written in.

See Assembly.GetManifestResourceNames() on MSDN.

Simply copy the relevant name, and use that instead of whatever you have defined in the variable 'resourceName'.

Notes - the resource name is case sensitive, and if you have incorrectly embedded the resource file, it will not show up in the list returned by the call to GetManifestResourceNames(). Also - make sure you are reading the resource from the correct assembly (if multiple assemblies are used) - it's all too easy to get the resources from the currently executing assembly rather than from a referenced assembly.

EDIT - .NET Core
Please see this SO post for details on how to embed using .NET Core.

Retrieving the manifest info looks to be similar - just use this.GetType().GetTypeInfo().Assembly.GetManifestResourceNames() to get the a manifest from the assembly where the code is executing.

I haven't figured out how to do the equivalent of Assembly.GetExecutingAssembly() in .NET Core yet! if anyone knows - please let me know and I will update this answer.


I had a similar issue check first that the file is included in your project , then go to properties and set the build action of that file to Embedded Resource . this worked for me .


The embedded file's "Build Action" property should be set as "Embedded Resource" to run the line, which is given below, properly:

Stream stream = assembly.GetManifestResourceStream(resourceName)

Right click on the file, click the property and then set "Build Action" property as "Embedded Resource":

enter image description here


Here is the cause of my null value.

http://adrianmejia.com/blog/2011/07/18/cs-getmanifestresourcestream-gotcha/

The GetManifestResourceStream method will always returns NULL if the resource ‘built action‘ property is not set to ‘embedded resource‘

After setting this property with all the needed files assembly.GetManifestResourceStream starts returning the correct stream instead of NULL.