“流不可读” [英] "Stream was not readable"

查看:134
本文介绍了“流不可读”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中使用流读取和写入文件处理。但是收到这个错误。





粘贴代码片段





我尝试了什么:



i到处使用了stream.close()我在整个程序的类函数中使用了流。

解决方案

将代码转发给我们就像这样粗鲁:将来只给我们相关的片段 - 方法错误最多发生。



但是......看看你的代码:

 FileStream fs =  new  FileStream(fileName,FileMode.Append,FileAccess.Write,FileShare.None); 
StreamReader sr = new StreamReader(fs);

当您打开Append文件时,您可以读取它:< a href =https://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.110).aspx> FileMode Enumeration(System.IO) [ ^ ]:

Quote:

打开文件(如果存在)并寻找文件末尾或创建一个新文件。这需要FileIOPermissionAccess.Append权限。 FileMode.Append只能与FileAccess.Write一起使用。努力寻找到一个位置的文件末尾抛出前一个IOException异常,任何尝试读取失败并引发NotSupportedException异常。如果你想读取的文件,打开它进行阅读,而不是追加或写。


猜测它似乎在googled中出现很多,你试图在它被处理后访问读取流。但这是一个猜测,因为你还没有发布代码片段。





c# - Stream不可读错误 - Stack Overflow [ ^ ]


i'm using stream reading and writing for file handling in c#. but getting this error.


Paste code snippet here



What I have tried:

i have used stream.close() everywhere where i have used streaming in my whole program's functions of classes.

解决方案

Dumping your code on us like that is rude: in future only give us the relevant fragment - the method the error occurs in at most.

But...look at your code:

FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None);
StreamReader sr = new StreamReader(fs);

When you open a file for Append, you can;t read it: FileMode Enumeration (System.IO)[^]:

Quote:

Opens the file if it exists and seeks to the end of the file, or creates a new file. This requires FileIOPermissionAccess.Append permission. FileMode.Append can be used only in conjunction with FileAccess.Write. Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws a NotSupportedException exception.

If you want to read the file, open it for Read, not Append or Write.


Guessing as it seems to appear alot when googled you are trying to access the read stream after it has been disposed. But it is a guess as you haven't posted a code snippet.


c# - Stream was not readable error - Stack Overflow[^]


这篇关于“流不可读”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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