Windows Dev Center

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.

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

virtual long long Seek(
	long long offset, 
	SeekOrigin origin
) override


Type: System::Int64

The point relative to origin from which to begin seeking.

Type: System.IO::SeekOrigin

Specifies the beginning, the end, or the current position as a reference point for offset, using a value of type SeekOrigin.

Return Value

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


An I/O error occurred.


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


Seeking is attempted before the beginning of the stream.


Methods were called after the stream was closed.

This method overrides Stream::Seek.


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 file and directory operations, 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.

using namespace System;
using namespace System::IO;
int main()
   String^ fileName =  "Test@##@.dat";

   // Create random data to write to the file. 
   array<Byte>^dataArray = gcnew array<Byte>(100000);
   (gcnew Random)->NextBytes( dataArray );
   FileStream^ fileStream = gcnew 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  -1;

      Console::WriteLine( "The data was written to {0} " 
      "and verified.", fileStream->Name );


The following example reads text in the reverse direction, from the end of file to the beginning of the file, by using the various SeekOrigin values with the Seek method.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
© 2015 Microsoft