C# .NET - How to set StreamReader file pointer to beginning

Asked By Jason Ye on 10-Dec-04 09:52 PM
I tried to use the following code to read a simple text file which contains only two lines: "Line 1" and "Line 2". I was expected the output as:

Line 1
Line 1

However, what I acctually got was:

Line 1
Line 2

It looks like "srFile.BaseStream.Seek(0, SeekOrigin.Begin);" sentence didn't work. Could anyone tell me what is the mistake I made? Thanks!

Jason

---

using System;
using System.IO;
using System.Text;

public class Test
{
	public static void Main(string[] args)
	{
		string strResult;

		FileStream fsFile = new FileStream("Test.dat", FileMode.Open); 
		StreamReader srFile = new StreamReader(fsFile, Encoding.Default);

		strResult = srFile.ReadLine();
		Console.WriteLine(strResult);

		// Set the StreamReader file pointer to the beginning.
		srFile.BaseStream.Seek(0, SeekOrigin.Begin);

		strResult = srFile.ReadLine();
		Console.WriteLine(strResult);
	}
}

never mind, figured out already - Asked By Jason Ye on 11-Dec-04 02:59 AM

...

So Jason - Asked By Peter Bromberg on 11-Dec-04 09:27 AM

Now that you have taken advantage of the magic of Eggheadcafe.com forums to discover the answer, how about considering the courtesy of  sharing your discovery with others who have been reading your post?

sure - Asked By Jason Ye on 15-Dec-04 01:17 AM

I have to use "srFile.DiscardBufferedData();" after reset the StreamReader pointer.