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.


使用属性来确定当前实例是否支持写入, CanSeek并使用属性来确定是否支持查找。 CanWriteUse 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.