尝试为流读取器初始化流 [英] trying to initialize stream for stream reader

查看:85
本文介绍了尝试为流读取器初始化流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码:

试试
{

列表< UsageProfile>结果=新列表< UsageProfile>();
System.Reflection.Assembly装配= System.Reflection.Assembly.GetExecutingAssembly();

如果(defaultUsageProfilePath!=")
使用(StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(defaultUsageProfilePath)))
{
while(!reader.EndOfStream)
result.Add(新的UsageProfile(reader.ReadLine()));
}
返回结果;
}
catch(ex ex例外)
{
返回null;

}

它在
停止
使用(StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(defaultUsageProfilePath)))

它说:

值不能为空.\ r \ n参数名称:stream

I am using this code:

try
{

List<UsageProfile> result = new List<UsageProfile>();
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

if (defaultUsageProfilePath != "")
using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(defaultUsageProfilePath)))
{
while (!reader.EndOfStream)
result.Add(new UsageProfile(reader.ReadLine()));
}
return result;
}
catch (Exception ex)
{
return null;

}

and it is stopping on

using (StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(defaultUsageProfilePath)))

and it is saying:

Value cannot be null.\r\nParameter name: stream

how can i initialize stream?

推荐答案

StreamReader类的构造函数将引发异常.这表示assembly.GetManifestResourceStream(defaultUsageProfilePath)返回null.
The constructor of the StreamReader class throws the exception. It means assembly.GetManifestResourceStream(defaultUsageProfilePath) is returning null.


您是否记得要在要检索的文件名的开头添加默认程序集?将程序集加载到反汇编程序中(ILDasm很好),以找出实际调用的资源,并确保您的字符串匹配.

错误告诉您StreamReader的参数为null,这是在资源加载器找不到所需资源时发生的情况.忘记默认情况下名称空间是默认的,这是资源查找失败的常见原因.
Did you remember to add the default assembly to the front of the file name you are trying to retrieve? Load your assembly into a disassembler (ILDasm is fine) to find out what the resource is actually called, and make sure your string matches.

The error is telling you the parameter to StreamReader is null, which is what happens when the resource loader can''t find the resource you ask for. Forgetting that the namespace gets prepended by default is the usual reason that resource lookups fail.


这篇关于尝试为流读取器初始化流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆