Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FileStream.Seek Method

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

[Visual Basic]
Overrides Public Function Seek( _
   ByVal offset As Long, _
   ByVal origin As SeekOrigin _
) As Long
[C#]
public override long Seek(
 long offset,
 SeekOrigin origin
);
[C++]
public: __int64 Seek(
 __int64 offset,
 SeekOrigin origin
);
[JScript]
public override function Seek(
   offset : long,
 origin : SeekOrigin
) : long;

Parameters

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

Return Value

The new position in the stream.

Exceptions

Exception Type Condition
IOException An I/O error occurs.
NotSupportedException The stream does not support seeking, such as if the FileStream is constructed from a pipe or console output.
ArgumentException Attempted seeking before the beginning of the stream.
ObjectDisposedException Methods were called after the stream was closed.

Remarks

This method overrides Seek.

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

Seeking to any location beyond the length of the stream is supported.

On some platforms, seeking past the end of a file and then doing a write operation results in undefined behavior.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file. File.Move

FileInfo.MoveTo

Copy a file. File.Copy

FileInfo.CopyTo

Get the size of a directory. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Create a subdirectory. CreateSubdirectory
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File
See the files in a directory. Name
Sort files in a directory by size. GetFileSystemInfos

Example

[Visual Basic, C#, C++] The following code example shows how to write data to a file, byte by byte, and then verify that the data was written correctly.

[Visual Basic] 
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

[C#] 
using System;
using System.IO;

class FStream
{
    static void Main()
    {
        const string fileName = "Test#@@#.dat";

        // Create random data to write to the file.
        byte[] dataArray = new byte[100000];
        new Random().NextBytes(dataArray);

        using(FileStream  
            fileStream = new FileStream(fileName, FileMode.Create))
        {
            // Write the data to the file, byte by byte.
            for(int i = 0; i < dataArray.Length; i++)
            {
                fileStream.WriteByte(dataArray[i]);
            }

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

            // Read and verify the data.
            for(int i = 0; i < fileStream.Length; i++)
            {
                if(dataArray[i] != fileStream.ReadByte())
                {
                    Console.WriteLine("Error writing data.");
                    return;
                }
            }
            Console.WriteLine("The data was written to {0} " +
                "and verified.", fileStream.Name);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

void main()
{
    String* fileName = "Test@##@.dat";

    // Create random data to write to the file.
    Byte dataArray __gc[]  = new Byte __gc[100000];
    (new Random())->NextBytes(dataArray);

    FileStream* fileStream = 
        new FileStream(fileName, FileMode::Create);
    try
    {
        // Write the data to the file, byte by byte.
        for(int i = 0; i < dataArray->Length; i++)
        {
            fileStream->WriteByte(dataArray[i]);
        }

        // Set the stream position to the beginning of the file.
        fileStream->Seek(0, SeekOrigin::Begin);

        // Read and verify the data.
        for(int i = 0; i < fileStream->Length; i++)
        {
            if(dataArray[i] != fileStream->ReadByte())
            {
                Console::WriteLine(S"Error writing data.");
                return;
            }
        }
        Console::WriteLine(S"The data was written to {0} " 
            S"and verified.", fileStream->Name);
    }
    __finally
    {
        fileStream->Close();
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

FileStream Class | FileStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft