Return StreamReader to Beginning

后端 未结 7 1544
醉梦人生
醉梦人生 2020-11-27 05:18

I\'m reading a file in line-by-line and I want to be able to restart the read by calling a method Rewind().

How can I manipulate my System.IO.Stre

7条回答
  •  难免孤独
    2020-11-27 05:38

    You need to seek on the stream, like you did, then call DiscardBufferedData on the StreamReader. Documentation here:

    Edit: Adding code example:

    Stream s = new MemoryStream();
    StreamReader sr = new StreamReader(s);
    // later... after we read stuff
    s.Position = 0;
    sr.DiscardBufferedData();        // reader now reading from position 0
    

提交回复
热议问题