C# FileStream Explained - Use the Seek() method
(Page 5 of 5 )
The FileStream class provides the Seek() method which changes the position of the stream in a relative way to one of three values: in relation to the beginning of the stream (using the enumeration SeekOrigin.Begin value), in relation to the current position of the stream (SeekOrigin.Current) and in relation to the end of the stream (SeekOrigin.End). Let's look at an example that explains how you can use this method.
using System;
using System.IO;
namespace MyStreams
{
class Class1
{
public static void Main()
{
try
{
using(FileStream fStream = File.OpenRead("aFile.txt"))
{
// using SeekOrigin.Begin
Console.WriteLine("Current Position: {0}",
fStream.Position);
Console.WriteLine("Calling Seek(1,SeekOrigin.Begin)");
fStream.Seek(1,SeekOrigin.Begin);
Console.WriteLine("Current Position: {0}",
fStream.Position);
Console.WriteLine("Reading bytes");
while(fStream.Position < fStream.Length)
{
Console.Write(" {0} ", fStream.ReadByte());
}
// using SeekOrigin.Current
fStream.Position = 0;
Console.WriteLine("nnCurrent Position: {0}",
fStream.Position);
Console.WriteLine("Calling Seek
(2,SeekOrigin.Current)");
fStream.Seek(2,SeekOrigin.Current);
Console.WriteLine("Current Position: {0}",
fStream.Position);
Console.WriteLine("Reading bytes");
while(fStream.Position < fStream.Length)
{
Console.Write(" {0} ", fStream.ReadByte());
}
// using SeekOrigin.End
fStream.Position = 0;
Console.WriteLine("nnCurrent Position: {0}",
fStream.Position);
Console.WriteLine("Calling Seek(-1,SeekOrigin.End)");
fStream.Seek(-1,SeekOrigin.End);
Console.WriteLine("Current Position: {0}",
fStream.Position);
Console.WriteLine("Reading bytes");
while(fStream.Position < fStream.Length)
{
Console.Write(" {0} ", fStream.ReadByte());
}
}
Console.ReadLine();
}
catch(IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

The idea of our example is to use the Seek() method to change the current position in the stream and then read the available bytes until we reach the end of the stream. As you know, our file contains the bytes 65 66 67 68. The first Seek method (Seek(1, SeekOrigin.Begin) moves the position of the stream by one relative to the beginning of the stream, causing the reading operation to read only the three remaining bytes. Note that we set the position of the stream using the FileStream.Position to zero before using the next Seek() method.
The second Seek() method moves the position by two relative to SeekOrigin.Current (currently equal to the first byte because we just assigned 0 to the Position property). This means that the read operation will begin at the third byte. The last Seek() method moves the position in the stream to -1 of the end of the stream (SeekOrigin.End), which means that it is reading only the last byte in the stream.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |