0

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?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
garm
  • 5
  • 2
  • Why do you want to read the file several times instead of just saving the result in-memory and refering to it there? Do you expect the file-contents to change? – sara Mar 20 '16 at 14:03

2 Answers2

0

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.

Kalten
  • 4,092
  • 23
  • 32
0

Try setting the BaseStream Position to 0, or copying the contents to a MemoryStream before actually start reading it.

Check out this thread: Return StreamReader to Beginning

Community
  • 1
  • 1
benyogyerek
  • 236
  • 2
  • 11