O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with Files: Take Two

While the use of StreamReader and StreamWriter is great to deal with text files, they fall short in providing byte-level access to a file. Other methods on the File class used to open or create files expose FileStream objects, which allow for richer manipulation of the data contained in a file. For example:

using (FileStream fs = File.OpenRead(@"c:\windows\system32\notepad.exe")){    var m = (char)fs.ReadByte();    var z = (char)fs.ReadByte();    Console.WriteLine("{0}{1}", m, z);}

This trivial example shows how to obtain a FileStream object that can read from the specified file. It uses the ReadByte method two times to read the first 2 bytes of an executable file, which are always MZ, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required