A stream may support reading, writing, or both.
If CanWrite returns false, the stream is read-only.
If CanRead returns false, the stream is write-only.
In the following example, we use a file stream to read, write, and seek:
using System;
using System.IO;
class Program
{
static void Main()
{
// Create a file called test.txt in the current directory:
using (Stream s = new FileStream("test.txt", FileMode.Create))
{
Console.WriteLine(s.CanRead); // True
Console.WriteLine (s.CanWrite); // True
Console.WriteLine (s.CanSeek); // True
s.WriteByte(101);
s.WriteByte(102);
byte[] block = { 1, 2, 3, 4, 5 };
s.Write(block, 0, block.Length);
Console.WriteLine(s.Length);
Console.WriteLine(s.Position);
s.Position = 0; // Move back to the start
Console.WriteLine(s.ReadByte());
Console.WriteLine(s.ReadByte());
Console.WriteLine (s.Read (block, 0, block.Length));
Console.WriteLine (s.Read (block, 0, block.Length));
}
}
}