// FileStream is simply a stream around a file, overriding Stream methods FileStream a = new FileStream(@"C:\Public\Test.txt", FileMode.Open); while (a.Position != a.Length) { Console.Write((char)a.ReadByte()); } a.Seek(0, SeekOrigin.Begin); StreamReader b = new StreamReader(a); Console.WriteLine(b.ReadToEnd()); b.Close();
// FileStream is simply a stream around a file, overriding Stream methods FileStream a = new FileStream(@"C:\Public\Test.txt", FileMode.Create); using (var writer = new StreamWriter(a)) { writer.AutoFlush = true; writer.WriteLine("Hello World"); } // The FileStream has been closed because of the using statement a = new FileStream(@"C:\Public\Test.txt", FileMode.Open); StreamReader b = new StreamReader(a); Console.WriteLine(b.ReadToEnd()); b.Close(); // Simple version if you only need to read the entire file Console.WriteLine(File.ReadAllText(@"C:\Public\Test.txt"));