Search code examples
c#streamreader

Read many of the StreamReader


I have little problem. My code in visual studio:

file = new StreamReader("D:\\BaseList.txt");
string line;
while ((line = file.ReadLine()) != null)
{
    listBox1.Items.Add(line);
}
file.Close(); // 1
file = new StreamReader("D:\\Baza3.txt");  //2

I read all lines in file and I would like once more to read from the beginning. Do I have to close the stream and reload the file to stream( line numbered 1 and 2)? Is there a method, which allows to set the stream at the beginning of my file without using this numbered line?


Solution

  • You can reset the position of the base stream like this

    streamReader.BaseStream.Position = 0;
    

    You can do that only if the base stream is seekable. (myStream.CanSeek == true). The is true in your case when you create a new StreamReader with a path string.