Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value cannot be null.Parameter name: stream error while reading the contents of embeddded file

Tags:

c#

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?

like image 994
pavan Avatar asked Jan 18 '23 08:01

pavan


1 Answers

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.

like image 75
Binoy Avatar answered Mar 15 '23 07:03

Binoy