Getmanifestresourcestream Returns Null

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.

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

C# GetManifestResourceStream returns null

If you've set correctly the build action to "embedded resource", the problem is most likely the name of the resource.

It should be namespace + file name; have you tried FormNamespace.Newtonsoft_Json.dll?

Otherwise, you can try executing the GetManifestResourceNames method and see what it returns.

GetManifestResourceStream() returning null

Make sure that your file's build action is "Embedded Resource".

If you file is in a folder you must include the folder's name.

For example if I create a folder "xx" in my WindowsFormsApplication1 project and add a file "dictionary.txt" ,the name will be
WindowsFormsApplication1.xx.dictionary.txt

In any case just use this to see all your resources

var files = Assembly.GetExecutingAssembly().GetManifestResourceNames();

GetManifestResourceStream() returning NULL in debugger

Assembly.GetCallingAssembly() is indeed the problem. When you are debugging, your code is being called by the Visual Studio Hosting process, yourapp.vshost.exe. Which of course does not contain your resource.

A workaround would be Project + Properties, Debug tab, untick the "Enable the Visual Studio hosting process option". But of course the real fix is to pay more attention to the location of the code vs the location of the resource, making GetExecutingAssembly() the most likely correct choice.

Why is GetManifestResourceStream() returning null?

A project consists of many files and Visual Studio allows you to select what happens for each file through its Build Action property.

In your case, the Program.cs file has the Build Action set to Compile. This instructs the build system to feed the file to the C# compiler as a source file containing C# code.

Another possible Build Action is the Embedded Resource option. This instructs the build system to take any file (regardless of its contents) and embed it as a binary blob somewhere in the resulting assembly file(s). If you intend a file to be accessed using the GetManifestResourceStream() method, this is the Build Action that enables you to do it.

Now if you want your file to be both compiled as a source file and embedded as a resource in the assembly, I don't know if the build system offers any options to combine two options for a single file. As a workaround, you can create a copy of your file and set one of the copies to Compile and one of the copies as an Embedded Resource. Notice that it might be a good idea to find a way to automate the task of this duplication, otherwise you may end up with supposedly duplicated files that are actually out of sync.



Related Topics



Leave a reply



Submit