System.IO.Stream.SetLength Method

When overridden in a derived class, sets the length of the current stream.


public abstract void SetLength (long value)


The desired length of the current stream in bytes.


NotSupportedExceptionThe stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.
ObjectDisposedExceptionThe stream is closed.
System.IO.IOExceptionAn I/O error occurred.


If the specified value is less than the current length of the stream, the stream is truncated. If the specified value is larger than the current length of the stream, the stream is expanded. If the stream is expanded, the contents of the stream between the old and the new length are not defined.

A stream must support both writing and seeking for SetLength to work.

Use the Stream.CanWrite property to determine whether the current instance supports writing, and the Stream.CanSeek property to determine whether seeking is supported.


Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,