Return StreamReader to Beginning

后端 未结 7 1549
醉梦人生
醉梦人生 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:34

    The question is looking for some kind of StreamReader.Rewind() method. You are looking for StreamReader.BaseStream.Position = 0; which sets the reader back to the beginning so it can be read again.

    StreamReader sr = new StreamReader("H:/kate/rani.txt");
    
    Console.WriteLine(sr.ReadToEnd());
    
    sr.BaseStream.Position = 0;
    
    Console.WriteLine("----------------------------------");
    
    while (!sr.EndOfStream)
    {
        Console.WriteLine(sr.ReadLine());
    }
    

提交回复
热议问题