System.IO.Stream
You should never create a Stream Object directly
The Stream abstract class represents a sequence of bytes going to or coming from a storage medium.
The storage medium could be a file or a physical or virtual device.
You can have file streams, memory streams, network streams and tape streams.
The basic operations for a stream are read, write and seek.
Before you can do anything with the stream you have to attach it to a reader or writer.
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("");
FileStream
System.IO.FileStream
Use the FileStream class to read from, write to, open, and close files on a file system
StreamReader and StreamWriter
The generic stream objects can read and write only individual bytes or groups of bytes.
It is often easier to work with more structured data such as lines of text or a specific value.
The StreamReader and StreamWriter classes allow you to work with strings of text in ANSI format.
The StreamReader and StreamWriter classes can work in conjunction with an Encoder object which determines how characters are encoded in the stream
Memory Streams
Custom Streams
Classes derived from Stream
System.IO.MemoryStream
System.IO.PipeStream
System.IO.BufferedStream
System.IO.UnmanagedMemoryStream
System.IO.Compression.DeflateStream
System.IO.Compression.GZipStream
System.Printing.PrintQueueStream
System.Data.SqlTypes.SqlFileStream
System.Net.Security.AuthenticatedStream
System.Net.Sockets.NetworkStream
System.Security.Cryptography.CryptoStream
System.IO.FileStream
Use the FileStream class to read from, write to, open, and close files on a file system
This is one of the classes derived from the Stream class.
File streams are buffered
Data is not written to a file when you write to the FileStream.
Once you have a filestream of the data you want you need to attach it to a Reader or Writer to be able to use it.
There are two options:
Plain Text - Use StreamReader and StreamWriter classes
Binary Data - Use BinaryReader and BinaryWriter classes
| BeginRead | start an asynchronous operation on the stream |
| BeginWrite | start an asynchronous operation on the stream |
| Close | Closes the stream |
| EndRead | To find out how many bytes were actually written |
| Flush | Empties the buferred stream and ensures all the contents are written |
| Handle | Returns the operating system file handle |
| Length | Returns the total size of the stream. You can change the length using SetLength |
| Lock | Lock a portion of the file |
| Position | determines the current position in the stream. You can change the position using Seek. |
| Read | Reads a number of bytes from the specified position into a Byte array. The ReadByte method reads and returns a single byte. |
| SetLength | Trims or extends the underlying file. |
| Unlock | Unlock a portion of the file |
| Write | Writes a number of bytes from an array into the stream. The WriteByte can write a single byte. |
You can check which operations are allowed by using the CanRead, CanWrite and CanSeek properties.
System.IO.FileStream oFileStream;
oFileStream = System.IO.FileStream("C:\Temp\textfile.txt",
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.Read);
if (oFileStream.CanRead == true)
{
}
System.IO.FileMode.Append | Create | CreateNew | Open | OpenOrCreate | Truncate
System.IO.FileAccess.Read | Write | ReadWrite
System.IO.FileShare.None | ReadWrite | Read | Write | Inheritable
Using File.OpenRead
This is another way of obtaining a FileStream object.
System.IO.FileStream oFileStream;
oFileStream = System.IO.File.OpenRead("C:\Temp\textfile.txt");
Stream Base Class
System.IO.Stream
System.IO.StreamReader
You use a StreamReader object to read from a text file.
There are a number of ways you can obtain the StreamReader object
The fifth line, lets the system decide the best type of encoding
System.IO.StreamReader oStreamReader;
oStreamReader = new System.IO.StreamReader(@"C:\\Temp\\textfile.txt");
oStreamReader = new System.IO.StreamReader(@"C:\\Temp\\textfile.txt", System.Text.Encoding.ASCII);
oStreamReader = new System.IO.StreamReader(@"C:\\Temp\\textfile.txt", System.Text.Encoding.Unicode);
oStreamReader = new System.IO.StreamReader(@"C:\\Temp\\textfile.txt", true);
Using a FileStream
System.IO.StreamReader oStreamReader;
System.IO.FileStream oFileStream;
oFileStream = new System.IO.FileStream(@"C:\\Temp\\textfile.txt", FileMode.Open);
oStreamReader = new System.IO.StreamReader(objFileStream);
Using a Stream
This lets you specify the mode, access and share
System.IO.StreamReader oStreamReader;
System.IO.Stream oStream;
oStream = System.IO.File.Open(@"C:\\Temp\\textfile.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
oStreamReader = new System.IO.StreamReader(oStream, System.Text.Encoding.UTF8);
Using a FileInfo
System.IO.StreamReader oStreamReader;
System.IO.FileInfo oFileInfo;
oFileInfo = new System.IO.FileInfo(@"C:\\Temp\\textfile.txt");
oStreamReader = objFileInfo.OpenText
Using File.OpenText
System.IO.StreamReader oStreamReader;
oStreamReader = System.IO.File.OpenText(@"C:\\Temp\\textfile.txt");
Reading the Contents
There are several ways to read from a StreamReader
ReadToEndAsyn
This can also be used to read all the remaining characters.
string sWholeFile;
// async
using (oStreamReader)
{
sWholeFile = await oStreamReader.ReadToEndAsync();
}
// sync
sWholeFile = objStreamReader.ReadToEnd;
ReadToEnd
ReadLine
string sLine;
string s = String.Empty;
while( (s = oStreamReader.ReadLine() ) != null);
{
}
Read
Read a Single Character
string sCharacter;
sCharacter = objStreamReader.Read;
Peek
The Peek method actually returns the code of the next character in the stream without actually extracting it, or it returns -1 if there are no more characters.
This method is used to test for the end of the file.
while (oStreamReader.Peek = -1)
{
read the stream
}
System.IO.StreamWriter
You use a StreamWriter object to write data to a text file.
There are a number of ways you can obtain the StreamWriter object
System.IO.StreamWriter oStreamWriter;
oStreamWriter = new System.IO.StreamWriter("C:\Temp\textfile.txt");
Using a FileStream
System.IO.StreamWriter oStreamWriter;
System.IO.FileStream oFileStream;
oFileStream = new System.IO.FileStream("C:\Temp\textfile.txt", FileMode.Open);
oStreamWriter = new System.IO.StreamWriter(objFileStream);
Using a Stream
This lets you specify the mode, access and share
System.IO.StreamWriter oStreamWriter;
System.IO.Stream oStream;
oStream = System.IO.File.Open("C:\Temp\textfile.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
oStreamWriter = new StreamWriter(objStream);
Using File.OpenWrite
System.IO.StreamWriter oStreamWriter;
System.IO.FileStream oFileStream;
oFileStream = System.IO.File.OpenWrite("C:\Temp\textfile.txt");
oStreamWriter = new System.IO.StreamWriter(objFileStream);
Creating a Text File
File.CreateText
System.IO.StreamWriter oStreamWriter;
oStreamWriter = System.IO.File.CreateText("C:\myfile.txt");
oStreamWriter.Write("this is some text");
oStreamWrite.Close;
Writing the Contents
There are several ways to write to a StreamWriter
Write a Single Line
This only works with strings and appends a newline character to the end.
string sText;
oStreamWriter.WriteLine(sText);
System.IO.BinaryReader
System.IO.BinaryReader
Binary files are not used that much anymore although it can still exist in legacy systems.
It is sometimes more efficient to store information in a random access binary file than in a database.
System.IO.BinaryWriter
System.IO.BinaryWriter
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext