Why Does Getmanifestresourcestream Returns Null While the Resource Name Exists When Calling Getmanifestresourcenames

Why does GetManifestResourceStream returns null (with dotnet core)

You can check what resource names are available:

string[] names = this.GetType().Assembly.GetManifestResourceNames();

And then if Resource exists write correct name of Resource

Assembly.GetManifestResourceStream always returns null

Your resource should be embedded:

Sample Image

You can make a little test to get all your resources and find the good name. After that your code seems correct.

 var allRessources= System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("fullpath here");
if (stream == null) return;

EDIT

To add a file in VS project as embedded resource: just add the file to your project, click on it, and then under Properties set Build Action to Embedded Resource. And that's it!

More information about embedded resource: https://support.microsoft.com/en-us/kb/319292#bookmark-4

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.

Can't load a manifest resource with GetManifestResourceStream()

The name of the resource is always:

<Base namespace>.<RelativePathInProject>.<FileName>

So if your resource is located in "Resources/Xsd/", and your default project namespace is "MonitoringAPI.Configuration", the resource name is:

"MonitoringAPI.Configuration.Resources.Xsd.MonitoringConfiguration.xsd"

Also make sure the build action for your resource is set to "Embedded Resource"

Remove prefixed namespace from embedded resource

You can use GetManifestResourceInfo to get additional information, such as the file name.

Taking your example, you might end up with something like the following:

string[] names = assembly.GetManifestResourceNames();

foreach (var name in names.Where(x => x.EndsWith("xsd")).ToList())
{
var info = assembly.GetManifestResourceInfo(name);
if (info != null)
{
var fileName = info.FileName;
// Do your stuff that needs filename here.
}

}

EDIT: This SO post might be relevant, if you find GetManifestResourceInfo returning null in these cases: Why does GetManifestResourceStream returns null while the resource name exists when calling GetManifestResourceNames?

The resources should be set to build action of "Embedded Resource", and there is the code security gotcha that is mentioned here: http://adrianmejia.com/blog/2011/07/18/cs-getmanifestresourcestream-gotcha/



Related Topics



Leave a reply



Submit