This documentation is archived and is not being maintained.

Stream.SetLength Method

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

[Visual Basic]
Public MustOverride Sub SetLength( _
   ByVal value As Long _
public abstract void SetLength(
 long value
public: virtual void SetLength(
 __int64 value
) = 0;
public abstract function SetLength(
   value : long


The desired length of the current stream in bytes.


Exception Type Condition
IOException An I/O error occurs.
NotSupportedException The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.
ObjectDisposedException Methods were called after the stream was 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.

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 CanWrite property to determine whether the current instance supports writing, and the CanSeek property to determine whether seeking is supported.


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

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