Why does GetManifestResourceStream returns null (with dotnet core)

Viewed 2079

Using dotnet core: .NET Core SDK Version: 3.1.102

Why is the following returning null
?

typeof(MyClassName).GetTypeInfo().Assembly.GetManifestResourceStream("MyFile.cs")

I verified the file exists in solution and has build action in properties 'Embedded Resource'

4 Answers

You can check what resource names are available:

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

And then if Resource exists write correct name of Resource

Just an update to .NET 5. What helped me to find out the name of the resources to help fix my issue in .NET 5 was the following:

string[] names = Assembly.GetCallingAssembly().GetManifestResourceNames();

                foreach (var name in names)
                {
                    Console.WriteLine(name);
                }

The name of the file must include the "root namespace" of the assembly, for example, "My.Namespace.MyFile.cs". Is is typically the name of the assembly, but it can be changed on the .csproj file.

Load the assembly where the resource is embedded, iterate through the GetManifestResourceNames to find the Fully Qualified Resource name which is then used to GetManifestResourceStream.

You can try

var assembly = Assembly.LoadFrom(Assembly.GetAssembly(typeof(MainWindow)).Location);
string[] names = assembly.GetManifestResourceNames();
var stream = assembly.GetManifestResourceStream(names[0]);
Related