Skip to main content
.NET Framework Class Library
SeekOrigin Enumeration

Specifies the position in a stream to use for seeking.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/883dhyx0_en-us_VS_110_1_0_0_0_0%]> _
<[%$TOPIC/883dhyx0_en-us_VS_110_1_0_0_0_1%](True)> _
Public Enumeration SeekOrigin
[[%$TOPIC/883dhyx0_en-us_VS_110_1_0_1_0_0%]]
[[%$TOPIC/883dhyx0_en-us_VS_110_1_0_1_0_1%](true)]
public enum SeekOrigin
[[%$TOPIC/883dhyx0_en-us_VS_110_1_0_2_0_0%]]
[[%$TOPIC/883dhyx0_en-us_VS_110_1_0_2_0_1%](true)]
public enum class SeekOrigin
[<[%$TOPIC/883dhyx0_en-us_VS_110_1_0_3_0_0%]>]
[<[%$TOPIC/883dhyx0_en-us_VS_110_1_0_3_0_1%](true)>]
type SeekOrigin
Members
Member nameDescription
Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store appsBeginSpecifies the beginning of a stream.
Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store appsCurrentSpecifies the current position within a stream.
Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store appsEndSpecifies the end of a stream.
Remarks

SeekOrigin is used by the Seek methods of Stream, BufferedStream, FileStream, MemoryStream, BinaryWriter, and other classes. The Seek methods take an offset parameter that is relative to the position specified by SeekOrigin.

Examples

The following example shows how to read backwards starting at the end of the stream, and how to read from a specified point in the stream.

Imports System
Imports System.IO

Public Class FSSeek
    Public Shared Sub Main()
        Dim offset As Long 
        Dim nextByte As Integer 

        ' alphabet.txt contains "abcdefghijklmnopqrstuvwxyz" 
        Using fs As New FileStream("c:\temp\alphabet.txt", FileMode.Open, FileAccess.Read)

            For offset = 1 To fs.Length
                fs.Seek(-offset, SeekOrigin.End)
                Console.Write(Convert.ToChar(fs.ReadByte()))
            Next offset
            Console.WriteLine()

            fs.Seek(20, SeekOrigin.Begin)

            nextByte = fs.ReadByte()
            While (nextByte > 0)
                Console.Write(Convert.ToChar(nextByte))
                nextByte = fs.ReadByte()
            End While
            Console.WriteLine()

        End Using 
    End Sub 
End Class 

' This code example displays the following output: 
' 
' zyxwvutsrqponmlkjihgfedcba 
' uvwxyz
using System;
using System.IO;

public class FSSeek
{
    public static void Main()
    {
        long offset;
        int nextByte;

        // alphabet.txt contains "abcdefghijklmnopqrstuvwxyz" 
        using (FileStream fs = new FileStream(@"c:\temp\alphabet.txt", FileMode.Open, FileAccess.Read))
        {
            for (offset = 1; offset <= fs.Length; offset++)
            {
                fs.Seek(-offset, SeekOrigin.End);
                Console.Write(Convert.ToChar(fs.ReadByte()));
            }
            Console.WriteLine();

            fs.Seek(20, SeekOrigin.Begin);

            while ((nextByte = fs.ReadByte()) > 0)
            {
                Console.Write(Convert.ToChar(nextByte));
            }
            Console.WriteLine();
        }
    }
}
// This code example displays the following output: 
// 
// zyxwvutsrqponmlkjihgfedcba 
// uvwxyz
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.