FileStream.Position Property

Definition

Gets or sets the current position of this stream.

public override long Position { get; set; }

Property Value

The current position of this stream.

Exceptions

The stream does not support seeking.

An I/O error occurred.

Attempted to set the position to a negative value.

Attempted seeking past the end of a stream that does not support this.

Examples

The following example uses the Length and Position properties to check for an end-of-file condition.

if( s.Length==s.Position )
{
   Console.WriteLine("End of file has been reached.");
}

Remarks

Seeking to any location beyond the length of the stream is supported. When you seek beyond the length of the file, the file size grows. Any data added to the end of the file is set to zero.

For a list of common file and directory operations, see Common I/O Tasks.

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
.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.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also