Information
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.SetLength Method

Sets the length of the current stream to the specified value.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public override void SetLength (
	long value
)
public void SetLength (
	long value
)
public override function SetLength (
	value : long
)
Not applicable.

Parameters

value

The value at which to set the length.

Exception typeCondition

NotSupportedException

The current stream is not resizable and value is larger than the current capacity.

-or-

The current stream does not support writing.

ArgumentOutOfRangeException

value is negative or is greater than the maximum length of the MemoryStream, where the maximum length is(MaxValue - origin), and origin is the index into the underlying buffer at which the stream starts.

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

If the specified value is less than the current length of the stream, the stream is truncated. If after the truncation the current position within the stream is past the end of the stream, the ReadByte method returns -1, the Read method reads zero bytes into the provided byte array, and Write and WriteByte methods append specified bytes at the end of the stream, increasing its length. If the specified value is larger than the current capacity and the stream is resizable, the capacity is increased, and the current position within the stream is unchanged. If the length is increased, the contents of the stream between the old and the new length are initialized to zeros.

NoteNote:

A MemoryStream instance must support writing for this method to work. Use the CanWrite property to determine whether the current instance supports writing. For additional information, see CanWrite.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft