using System; using System.IO; class Program { static void Main() { FileStream fs = new FileStream("file.txt", FileMode.OpenOrCreate, FileAccess.Read); int byteData; while((byteData = fs.ReadByte()) != -1) { Console.WriteLine(byteData); } fs.Close(); } }
using System; using System.IO; class Program { static void Main() { byte[] buffer = new byte[4]; MemoryStream ms = new MemoryStream(buffer); ms.WriteByte(1); ms.WriteByte(2); ms.WriteByte(3); ms.WriteByte(4); ms.Position = 0; int byteData1 = ms.ReadByte(); int byteData2 = ms.ReadByte(); Console.WriteLine(byteData1); Console.WriteLine(byteData2); } }This example creates a memory stream and writes four bytes of data to it. It then reads the first two bytes of the stream and prints them out. The System.IO Stream class is part of the .NET Framework Class Library, which is a package library provided by Microsoft for C# programming.