SeekOrigin Enumeration

System.IO

Specifies the starting position in a stream to which a stream is seeking.

Summary

The SeekOrigin enumeration is used to define the reference point for seeking operations within a stream. When you want to move the current position within a file or other stream, you specify how that new position should be calculated relative to a particular point. This enumeration provides those standard reference points.

Members

Members

Begin (0)
Specifies that the seek is performed from the beginning of the stream.
Current (1)
Specifies that the seek is performed from the current position of the stream.
End (2)
Specifies that the seek is performed from the end of the stream.

Remarks

The SeekOrigin enumeration is commonly used with the Stream.Seek method to reposition the read/write pointer within a stream. For example, to move to the 100th byte from the beginning of a file, you would use stream.Seek(100, SeekOrigin.Begin). To move back 50 bytes from the current position, you would use stream.Seek(-50, SeekOrigin.Current).

The exact behavior of seeking from the end of the stream (SeekOrigin.End) can vary depending on the specific stream implementation. For most file streams, seeking from the end means the offset is relative to the end, and a negative offset moves backwards.

Example

// This example demonstrates seeking to different positions in a file stream.
using System;
using System.IO;

public class SeekOriginExample
{
    public static void Main(string[] args)
    {
        string filePath = "example.txt";

        // Create a dummy file for demonstration
        using (StreamWriter writer = new StreamWriter(filePath))
        {
            writer.WriteLine("This is the first line.");
            writer.WriteLine("This is the second line.");
            writer.WriteLine("This is the third line.");
        }

        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            // Seek to the beginning of the file
            fs.Seek(0, SeekOrigin.Begin);
            Console.WriteLine($"Position after seeking to Begin: {fs.Position} bytes.");

            // Seek to 10 bytes from the beginning
            fs.Seek(10, SeekOrigin.Begin);
            Console.WriteLine($"Position after seeking 10 bytes from Begin: {fs.Position} bytes.");

            // Read some data to change the current position
            byte[] buffer = new byte[10];
            fs.Read(buffer, 0, 5);
            Console.WriteLine($"Position after reading 5 bytes: {fs.Position} bytes.");

            // Seek 15 bytes forward from the current position
            fs.Seek(15, SeekOrigin.Current);
            Console.WriteLine($"Position after seeking 15 bytes from Current: {fs.Position} bytes.");

            // Seek to the end of the file and then back 5 bytes
            fs.Seek(-5, SeekOrigin.End);
            Console.WriteLine($"Position after seeking -5 bytes from End: {fs.Position} bytes.");
        }

        // Clean up the dummy file
        File.Delete(filePath);
    }
}

This example shows how to use SeekOrigin.Begin, SeekOrigin.Current, and SeekOrigin.End to control the position within a FileStream.