FileStream.SetLength(Int64) FileStream.SetLength(Int64) FileStream.SetLength(Int64) FileStream.SetLength(Int64) Method


将该流的长度设置为给定值。Sets the length of this stream to the given value.

 override void SetLength(long value);
public override void SetLength (long value);
override this.SetLength : int64 -> unit
Public Overrides Sub SetLength (value As Long)


Int64 Int64 Int64 Int64

流的新长度。The new length of the stream.


发生了 I/O 错误。An I/O error has occurred.

该流不支持写入和查找。The stream does not support both writing and seeking.

试图将 value 参数设置为小于 0。Attempted to set the value parameter to less than 0.


此方法重写 SetLengthThis method overrides SetLength.

如果给定的值小于当前流的长度,则流被截断。If the given value is less than the current length of the stream, the stream is truncated. 在此方案中,如果当前的位置大于新的长度,当前位置移动到流的最后一个字节。In this scenario, if the current position is greater than the new length, the current position is moved to the last byte of the stream. 如果给定的值大于流的当前长度,则流被扩展,以及当前的位置保持不变。If the given value is larger than the current length of the stream, the stream is expanded, and the current position remains the same. 如果流已展开,旧的和新的长度之间的流的内容未定义。If the stream is expanded, the contents of the stream between the old and the new length are undefined.

流必须支持写入和查找有关SetLength工作。A stream must support both writing and seeking for SetLength to work.


使用CanWrite属性来确定当前实例是否支持写入,和CanSeek属性来确定是否支持查找。Use the CanWrite property to determine whether the current instance supports writing, and the CanSeek property to determine whether seeking is supported. 有关更多信息,请参阅 CanWriteCanSeekFor additional information, see CanWrite and CanSeek.

常见的文件和目录操作的列表,请参阅通用 I/O 任务For a list of common file and directory operations, see Common I/O Tasks.