Windows apps
Collapse the table of content
Expand the table of content
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.

MemoryStream.Seek Method

Sets the position within the current stream to the specified value.

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


The new position within the stream. This is relative to the loc parameter, and can be positive or negative.
A value of type SeekOrigin, which acts as the seek reference point.

Return Value

The new position within the stream, calculated by combining the initial reference point and the offset.


Exception Type Condition
IOException Seeking is attempted before the beginning of the stream.
ArgumentOutOfRangeException offset is greater than MaxValue.
ArgumentException There is an invalid SeekOrigin.
ObjectDisposedException The current stream instance is closed.


For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

This method overrides Seek.

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


[Visual Basic, C#, C++] This code example is part of a larger example provided for the MemoryStream class.

[Visual Basic] 
' Set the stream position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin)

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

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

[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.


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

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

© 2017 Microsoft