简体   繁体   English

值不能为null。参数名称:读取嵌入文件内容时出现流错误

[英]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.

相关问题 调整JPEG品质时出错:值不能为空参数名称:结构 - Error on adjusting jpeg quality: Value cannot be null.Parameter name: structure 值不能为null参数名称:在ASP.NET中进行数据绑定时的键 - Value cannot be null.Parameter name: key when databind in ASP.NET 无法使用 ASP.NET Identity 登录,值不能为空。参数名称:manager - Can not sign in using ASP.NET Identity, Value cannot be null.Parameter name: manager “row”参数不能为空。参数名称:row - 'row' argument cannot be null.Parameter name: row RestSharp“获取响应流时出错(ReadAsync):ReceiveFailure Value不能为null。参数名称:src“ - RestSharp “Error getting response stream (ReadAsync): ReceiveFailure Value cannot be null. Parameter name: src” Nhibernate ArgumentNullException 未处理值不能为空参数名称:流 - Nhibernate ArgumentNullException was unhandled Value cannot be null Parameter name: stream PDF Checkbox Error:值不能为null。 参数名称:值 - PDF Checkbox Error : Value cannot be null. Parameter name: value 添加WebService参考时出错:值不能为null。 参数名称:DiscoveryError&mexError - Error While Adding WebService Reference : Value cannot be null. Parameter name: discoveryError & mexError 在项目 Xamarin.Forms 中构建时出现错误“值不能为空参数名称:方​​法” - Error “Value cannot be null parameter name: method” while building in project Xamarin.Forms WPF 设计器错误值不能为空。 参数名称:模式 - WPF designer error Value cannot be null. Parameter name: pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM