This documentation is archived and is not being maintained.

FileStream.Seek Method

Sets the current position of this stream to the given value.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Overrides Function Seek ( _
	offset As Long, _
	origin As SeekOrigin _
) As Long

Parameters

offset
Type: System.Int64
The point relative to origin from which to begin seeking.
origin
Type: System.IO.SeekOrigin
Specifies the beginning, the end, or the current position as a reference point for origin, using a value of type SeekOrigin.

Return Value

Type: System.Int64
The new position in the stream.

ExceptionCondition
IOException

An I/O error occurred.

NotSupportedException

The stream does not support seeking, such as if the FileStream is constructed from a pipe or console output.

ArgumentException

Seeking is attempted before the beginning of the stream.

ObjectDisposedException

Methods were called after the stream was closed.

This method overrides Stream.Seek.

NoteNote

Use the FileStream.CanSeek property to determine whether the current instance supports seeking. For additional information, see Stream.CanSeek.

You can seek to any location beyond the length of the stream. When you seek beyond the length of the file, the file size grows. In Windows NT and later versions, data added to the end of the file is set to zero. In Windows 98 or earlier versions, data added to the end of the file is not set to zero, which means that previously deleted data is visible to the stream.

For a list of common I/O tasks, see Common I/O Tasks.

The following example shows how to write data to a file, byte by byte, and then verify that the data was written correctly.


Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Text

Class FStream

    Shared Sub Main()

        Const fileName As String = "Test#@@#.dat"

        ' Create random data to write to the file.
        Dim dataArray(100000) As Byte
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(dataArray)

        Dim fileStream As FileStream = _
            new FileStream(fileName, FileMode.Create)
        Try

            ' Write the data to the file, byte by byte.
            For i As Integer = 0 To dataArray.Length - 1
                fileStream.WriteByte(dataArray(i))
            Next i

            ' Set the stream position to the beginning of the stream.
            fileStream.Seek(0, SeekOrigin.Begin)

            ' Read and verify the data.
            For i As Integer = 0 To _
                CType(fileStream.Length, Integer) - 1

                If dataArray(i) <> fileStream.ReadByte() Then
                    Console.WriteLine("Error writing data.")
                    Return
                End If
            Next i
            Console.WriteLine("The data was written to {0} " & _
                "and verified.", fileStream.Name)
        Finally
            fileStream.Close()
        End Try

    End Sub
End Class


The following example reads text from a file backwards by using the various SeekOrigin values with the Seek method.


Imports System
Imports System.IO

Public Class FSSeek
    Public Shared Sub Main()
        ' temp.txt contains "This is a test!"

        Dim fs As New FileStream("c:\temp\temp.txt", FileMode.Open, FileAccess.Read)
        Dim offset As Long

        ' read the file backwards using SeekOrigin.Begin...
        For offset = fs.Length - 1 To 0 Step -1
            fs.Seek(offset, SeekOrigin.Begin)
            Console.Write(Convert.ToChar(fs.ReadByte()))
        Next
        Console.WriteLine()

        ' read the file backwards using SeekOrigin.End...
        For offset = 1 To fs.Length
            fs.Seek(-offset, SeekOrigin.End)
            Console.Write(Convert.ToChar(fs.ReadByte()))
        Next offset
        Console.WriteLine()

        ' read the file backwards using SeekOrigin.Current...
        fs.Seek(0, SeekOrigin.End)
        For offset = 0 To fs.Length - 1
            fs.Seek(-1, SeekOrigin.Current)
            Console.Write(Convert.ToChar(fs.ReadByte()))
            fs.Seek(-1, SeekOrigin.Current)
        Next offset
        Console.WriteLine()
        fs.Close()
    End Sub
End Class

' This code example displays the following output:
'
' !tset a si sihT
' !tset a si sihT
' !tset a si sihT


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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