我是否需要将流(C#)重置回开始?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1746092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:26:15  来源:igfitidea点击:

Do I need to reset a stream(C#) back to the start?

c#.net

提问by chobo2

I don't know too much about streams in C#. Right now I have a stream that I put into a stream reader and read it. Later on in some other method I need to read the stream(same stream object) but this time I get this error

我不太了解 C# 中的流。现在我有一个流,我放入流阅读器并阅读它。稍后在其他一些方法中,我需要读取流(相同的流对象),但这次我收到此错误

System.ArgumentException was unhandled by user code
  Message="Stream was not readable."
  Source="mscorlib"
  StackTrace:
       at System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
       at System.IO.StreamReader..ctor(Stream stream)
       at ExtractTitle(Stream file) in :line 33
       at GrabWebPage(String webPath) in :line 62
       at lambda_method(ExecutionScope , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 

So I am thinking maybe by reading the stream it goes to the end. Then when I try to read it again it is at the end of the stream and thats why I am getting this error.

所以我想也许通过阅读它到达最后的流。然后,当我再次尝试读取它时,它位于流的末尾,这就是我收到此错误的原因。

So can anyone shed some light on this?

那么任何人都可以对此有所了解吗?

Thanks

谢谢

采纳答案by chobo2

When you read a stream to the end, specifically with StreamReader's ReadToEndmethod, you have to Seekit back to the beginning. This can be done like so:

当您将流读到最后时,特别是使用StreamReader'sReadToEnd方法时,您必须将Seek其返回到开头。这可以像这样完成:

StreamReader sr = new StreamReader(stream);
sr.ReadToEnd();
stream.Seek(0, SeekOrigin.Begin); //StreamReader doesn't have the Seek method, stream does.
sr.ReadToEnd(); // This now works

回答by Michael Petrotta

Your conclusion is correct; once you've reached the end of your stream, you won't be able to read more data until you've reset your position within the stream:

你的结论是正确的;一旦到达流的末尾,您将无法读取更多数据,直到您在流中重置位置:

myStream.Position = 0;

This is equivalent to seeking back to the beginning. Note that your stream must support seeking for this to work; not all streams do. You can check this with the CanSeekproperty.

这相当于找回原点。请注意,您的流必须支持寻求此功能;并非所有流都可以。您可以向CanSeek酒店确认这一点。

回答by Glaucio BP

Use BaseStreamfor StreamReader:

使用BaseStreamStreamReader

 StreamReader sr = new StreamReader(pFileStream);
 sr.BaseStream.Seek(0, SeekOrigin.Begin);