FileSystem.Seek Method (Int32)

Returns a Long specifying the current read/write position in a file opened by using the FileOpen function, or sets the position for the next read/write operation in a file opened by using the FileOpen function. The My feature gives you better productivity and performance in file I/O operations than Seek. For more information, see FileSystem.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static long Seek(
	int FileNumber
)

Parameters

FileNumber
Type: System.Int32

Required. An Integer that contains a valid file number.

Return Value

Type: System.Int64
Returns a Long specifying the current read/write position in a file opened by using the FileOpen function, or sets the position for the next read/write operation in a file opened by using the FileOpen function.

ExceptionCondition
IOException

FileNumber does not exist.

IOException

File mode is invalid.

Seek returns a value between 1 and 2,147,483,647 (equivalent to 2^31 – 1), inclusive.

The following describes the return values for each file access mode:

Mode

Return Value

Random

Number of the next record read or written

Binary, Input, Output, Append

Byte position at which the next operation occurs. The first byte in a file is at position 1, the second byte is at position 2, and so on.

This example uses the Seek function to return the current file position. The example assumes TestFile is a file that contains records of the structure Record.

Structure Record   ' Define user-defined type.
    Dim ID As Integer 
    Dim Name As String 
End Structure

For files opened in Random mode, Seek returns the number of next record.

FileOpen(1, "TESTFILE", OpenMode.Random)
Do While Not EOF(1)
    WriteLine(1, Seek(1))   ' Write record number.
    FileGet(1, MyRecord, -1)   ' Read next record.
Loop
FileClose(1)

For files opened in modes other than Random mode, Seek returns the byte position at which the next operation occurs. Assume TestFile is a file that contains several lines of text.

' Report character position at beginning of each line. 
Dim TextLine As String
FileOpen(1, "TESTFILE", OpenMode.Input)   ' Open file for reading.
While Not EOF(1)
    ' Read next line.
    TextLine = LineInput(1)
    ' Position of next line.
    MsgBox(Seek(1))
End While
FileClose(1)

This example uses the Seek function to set the position for the next read or write in a file.

For files opened in modes other than Random mode, Seek sets the byte position at which the next operation occurs. Assume TestFile is a file that contains several lines of text.

Dim someText As String = "This is a test string." 
' Open file for output.
FileOpen(1, "TESTFILE", OpenMode.Input)
' Move to the third character.
Seek(1, 3)
Input(1, someText)
Console.WriteLine(someText)
FileClose(1)

.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

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft