Last Updated:

Input and output in .NET  Streaming classes

Roughly summarizing, you can divide the I/O functions in the .NET Framework into two broad categories that are independent of the storage device (disk, memory, etc.): write and read.

Data can be thought of as a stream of bytes or characters. For example, we could read 500 bytes from a file, and write them to the memory buffer. Data can also be thought of as a set of objects. Reading and writing objects are called, respectively, conversion from serial to parallel form and conversion to serial form (serialization). We can serialize (write) a list of Customer objects to disk. We can then convert the list of Customer objects from a sequential form to a parallel form, i.e. read the list back into memory.

The System::IO namespace has several read-write classes that allow you to use a variety of storage devices as long as the data can be treated as bytes or characters. Serialization functionality can occur in various places in the .NET framework. The System::Runtime::Serialization namespace is used for serialization to the Common Type System. The System::Xml::Serialization namespace is used to serialize XML documents.

Streaming Classes

Stream is an abstract class that is the base for reading and writing bytes to some data store of the file type. This class supports synchronous and asynchronous reading and writing. Asynchronous methods are discussed later in this chapter. The Stream class contains typical and expected methods from such a class: ReadWriteSeekFlush, and Close.

The FileStream class, which derives from the Stream class (Stream, Abstract Sequential File), provides operations for reading and writing a sequence of bytes to a file. The FileStream constructor creates an instance of the stream. The overloaded methods of the Stream class (Stream, Abstract Sequential File) read and write to the file.

The Stream class has other derived classes: MemoryStreamBufferedStream, and NetworkStream (in System::Net::Sockets).

The FileStream sample (in the FilelO folder with the I/O samples) illustrates how to use the Stream stream class. If the file does not exist, a new one is created, and then numbers from 0 to 9 are written to the file. If the file already exists, the program reads the 5 bytes at the end of the file and then prints them to the console. (The example must be run twice; the first time the program will create a file and write numbers to it, and the second time it will read and print part of the file.)

unsigned char data _gc[] =
// garbage collector - data - unsigned characters
new unsigned char_gc[10];
// garbage collector - new unsigned character
FileStream *fs = new FileStream(
if (fs › Length == 0)
// if Length == 0
Console::WriteLine("Writing Data...");
// Write data
for (short i = 0; i < 10; i++) data[i] = (unsigned char)i;
// data = unsigned character
fs › Write(data, 0, 10);
// Write data
fs › Seek(-5, SeekOrigin::End);
// Look for the end
int count = fs › Read(data, 0, 10);
// Read data
for (int i = 0; i < count; i++)
// check
// data
fs › Close();