MemoryStream.Write 方法

定義

多載

Write(ReadOnlySpan<Byte>)

source 中包含的位元組序列寫入目前的記憶體資料流,並將記憶體資料流中目前的位置依寫入的位元組數往前移。

Write(Byte[], Int32, Int32)

使用讀取自緩衝區的資料,將位元組區塊寫入至目前的資料流。

Write(ReadOnlySpan<Byte>)

source 中包含的位元組序列寫入目前的記憶體資料流,並將記憶體資料流中目前的位置依寫入的位元組數往前移。

public:
 override void Write(ReadOnlySpan<System::Byte> source);
public:
 override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write (ReadOnlySpan<byte> source);
public override void Write (ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (source As ReadOnlySpan(Of Byte))
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

參數

sourcebuffer
ReadOnlySpan<Byte>

記憶體區域。 這個方法會將此區域的內容複製到目前的記憶體資料流。

適用於

Write(Byte[], Int32, Int32)

使用讀取自緩衝區的資料,將位元組區塊寫入至目前的資料流。

public:
 override void Write(cli::array <System::Byte> ^ buffer, int offset, int count);
public override void Write (byte[] buffer, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)

參數

buffer
Byte[]

寫入資料的來源緩衝區。

offset
Int32

buffer 中以零起始的位元組位移,即開始將位元組複製到目前資料流的位置。

count
Int32

寫入的最大位元組數。

例外狀況

buffernull

資料流不支援寫入。 如需詳細資訊,請參閱 CanWrite

-或- 和 count 位元組相比,目前的位置更接近資料流末端,並且容量無法修改。

緩衝區的長度減去 offset 小於 count

offsetcount 為負值。

發生 I/O 錯誤。

目前的資料流執行個體已關閉。

範例

此程式碼範例是提供給 類別之較大範例的 MemoryStream 一部分。

// Write the first string to the stream.
memStream->Write( firstString, 0, firstString->Length );
// Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length);
' Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length)

備註

這個方法會覆寫 Write

參數 offset 會提供 中 buffer 要寫入之第一個位元組的位移,而 count 參數會提供要寫入的位元組數目。 如果寫入作業成功,資料流程中的目前位置會由寫入的位元組數目進階。 如果發生例外狀況,資料流程中的目前位置不會變更。

MemoryStream除了使用 byte[] 參數建構的 之外,在 展開 MemoryStream 結尾的 MemoryStream 寫入作業。

另請參閱

適用於