6

I am doing an application in c#.

I get the error:

Value cannot be null. Parameter name: stream while reading the contentxs of embedded file

in the code

Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);

How to solve this error?

Dr Rob Lang
  • 6,659
  • 5
  • 40
  • 60
pavan
  • 149
  • 1
  • 6
  • 15

2 Answers2

8

What I found is as once I faced this problem after adding a text file or template file to read the email body and than reading the path to that template file using below syntax:

Assembly asm = Assembly.GetExecutingAssembly();
string assemblyName = asm.GetName().Name;
string emailTemplateName = xyz.tt;
emailTemplateName = assemblyName + "." +  emailTemplateName;
using (StreamReader reader = new StreamReader(asm.GetManifestResourceStream(emailTemplateName)))
{
body = reader.ReadToEnd();
}

The file by default gets added to project library with property "Build Action"= Content. I changed value from "Content" to "Embedded Resource" and everything worked good.

Binoy
  • 390
  • 5
  • 19
  • 3
    Changing the build action to "Embedded Resource" on the files properties in Visual Studio fixed it for me. Thanks. – Andrew Hill Jan 10 '19 at 09:52
5

The prefix to embedded resources is not the assembly name, but rather: the default namespace specified on the project. Your best tactic, though, is to look at:

string[] names = Asm.GetManifestResourceNames();
foreach(var name in names) Debug.WriteLine(name);

and see what the names actually are, and tweak the prefix accordingly. You will get null if it is not a complete match.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900