[英]Value cannot be null.Parameter name: stream error while reading the contents of embeddded file
I am doing an application in c#. 我正在用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? 如何解决这个错误?
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. 如果不完全匹配,您将获得
null
。
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. 默认情况下,该文件将添加到项目库中,其属性为“Build Action”= Content。 I changed value from "Content" to "Embedded Resource" and everything worked good.
我将价值从“内容”改为“嵌入式资源”,一切都运行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.