MemoryStream.Seek(Int64, SeekOrigin) Method

Definition

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

C#
public override long Seek(long offset, System.IO.SeekOrigin loc);

Parameters

offset
Int64

The new position within the stream. This is relative to the loc parameter, and can be positive or negative.

loc
SeekOrigin

A value of type SeekOrigin, which acts as the seek reference point.

Returns

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

Exceptions

Seeking is attempted before the beginning of the stream.

There is an invalid SeekOrigin.

-or-

offset caused an arithmetic overflow.

The current stream instance is closed.

Examples

This code example is part of a larger example provided for the MemoryStream class.

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

Remarks

This method overrides Stream.Seek.

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

Do not use the Seek method to determine the new position in the stream if the MemoryStream was initialized with a non-zero offset. If you do, Seek will return an inaccurate value. Instead, use the Position property to get the new position of the stream.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also