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.

資料流不支援寫入和搜尋,例如,如果資料流是從管道或主控台 (Console) 輸出所建構。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.

使用 CanWrite 屬性來判斷目前的實例是否支援寫入,以及 CanSeek 屬性,以判斷是否支援搜尋。Use the CanWrite property to determine whether the current instance supports writing, and the CanSeek property to determine whether seeking is supported.