Ron Ron - 11 months ago 213
C# Question

GetManifestResourceStream returns NULL

This is a C# .NET 4.0 application:

I'm embedding a text file as a resource and then trying to display it in a dialog box:

var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyProj.Help.txt";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
string result = reader.ReadToEnd();
System.Windows.Forms.MessageBox.Show(result, "MyProj", MessageBoxButtons.OK);

The solution is MyProjSolution and the executable is MyProj.exe. Help.txt is an embedded resource. However, the stream is null. I've tried MyProjSolution.Help.txt and MyProjSolution.MyProj.Help.txt but nothing seems to work.

Jay Jay
Answer Source

You can check that the resources are correctly embedded by using

//From the assembly where this code lives!

//or from the entry point to the application - there is a difference!

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


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 to easy to get the resources from the currently executing assembly rather than from a referenced assembly.