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);
}
}