StreamReader 参数NullException
嘿,
我有一个使用 sql Express 本地数据库的程序。我希望能够使用该程序来更新该数据库以运行必要的脚本。文本文件已作为嵌入资源添加到项目(VS2010)中,并且该文件包含文本。但它无法打开该文件。我收到“ArgumentNullException 未处理”“值不能为空。参数名称:流”这是代码...
Assembly assem;
StreamReader textReader;
assem = Assembly.GetExecutingAssembly();
//fails at this line below.
textReader = new StreamReader(assem.GetManifestResourceStream("projectName.sqlUpdates.txt"));
tReader.Peek() != -1)
script = textReader.ReadToEnd();
Hey there,
I have a program that uses a sql express local DB. I want to be able to update that DB using the program to run the necessary scripts. A text files has been added as an embedded resource to the project (VS2010), and the file contains text. It fails to open the file though. I get an "ArgumentNullException was unhandled" "Value cannot be null. Parametername: stream" here's the code...
Assembly assem;
StreamReader textReader;
assem = Assembly.GetExecutingAssembly();
//fails at this line below.
textReader = new StreamReader(assem.GetManifestResourceStream("projectName.sqlUpdates.txt"));
tReader.Peek() != -1)
script = textReader.ReadToEnd();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“projectName”是文件 sqlUpdates.txt 所在的完整程序集和命名空间吗?代码是在“projectName”程序集中还是其他地方运行上面的示例?
如果 sqlUpdates.txt 文件位于 projectName 程序集下方的命名空间中,则需要在对 GetManizestResourceStream 的调用中指定该名称空间,即:
Is 'projectName' the full assembly and namespace where the file sqlUpdates.txt lives? Is the code running your example above in the 'projectName' assembly, or somewhere else?
If the sqlUpdates.txt file lives in a namespace below the projectName assembly, then you need to specify that in the call to GetManifiestResourceStream, ie:
这基本上意味着
返回 null...如果找不到该资源,它将执行此操作。检查它是否确实在程序集中,例如使用 Reflector 或assem.GetManifestResourceNames()。
That basically means that
returned null... which it will do if it can't find that resource. Check that it's actually in the assembly, e.g. with Reflector or
assem.GetManifestResourceNames()
.