Stream.SetLength(Int64) 方法


当在派生类中重写时,设置当前流的长度。When overridden in a derived class, sets the length of the current stream.

 abstract void SetLength(long value);
public abstract void SetLength (long value);
abstract member SetLength : int64 -> unit
Public MustOverride Sub SetLength (value As Long)



所需的当前流的长度(以字节表示)。The desired length of the current stream in bytes.


出现 I/O 错误。An I/O error occurs.

流不支持写入和查找,例如在流通过管道或控制台输出构造的情况下即为如此。The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.

在流关闭后调用方法。Methods were called after the stream was closed.


如果指定的值小于流的当前长度,则流将被截断。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.

流必须支持写入和查找SetLength , 才能正常工作。A stream must support both writing and seeking for SetLength to work.

使用属性来确定当前实例是否支持写入, CanSeek并使用属性来确定是否支持查找。 CanWriteUse the CanWrite property to determine whether the current instance supports writing, and the CanSeek property to determine whether seeking is supported.