MemoryStream.Write 方法

定義

多載

Write(ReadOnlySpan<Byte>)

source 中包含的位元組序列寫入至目前的記憶體資料流,並將此記憶體資料流中目前的位置依寫入的位元組數向前移動。Writes the sequence of bytes contained in source into the current memory stream and advances the current position within this memory stream by the number of bytes written.

Write(Byte[], Int32, Int32)

使用讀取自緩衝區的資料,將位元組區塊寫入至目前的資料流。Writes a block of bytes to the current stream using data read from a buffer.

Write(ReadOnlySpan<Byte>)

source 中包含的位元組序列寫入至目前的記憶體資料流,並將此記憶體資料流中目前的位置依寫入的位元組數向前移動。Writes the sequence of bytes contained in source into the current memory stream and advances the current position within this memory stream by the number of bytes written.

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

參數

source
ReadOnlySpan<Byte>

記憶體區域。A region of memory. 這個方法會將此區域的內容複製到目前的記憶體資料流。This method copies the contents of this region to the current memory stream.

Write(Byte[], Int32, Int32)

使用讀取自緩衝區的資料,將位元組區塊寫入至目前的資料流。Writes a block of bytes to the current stream using data read from a buffer.

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[]

寫入資料的來源緩衝區。The buffer to write data from.

offset
Int32

buffer 中以零起始的位元組位移,即開始將位元組複製到目前資料流的位置。The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

count
Int32

寫入的最大位元組數。The maximum number of bytes to write.

例外狀況

buffernullbuffer is null.

資料流不支援寫入。The stream does not support writing. 如需詳細資訊,請參閱 CanWriteFor additional information see CanWrite.

-或--or- count 位元組相比,目前的位置更接近資料流末端,並且容量無法修改。The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified.

緩衝區的長度減去 offset 小於 countoffset subtracted from the buffer length is less than count.

offsetcount 為負值。offset or count are negative.

發生 I/O 錯誤。An I/O error occurs.

目前的資料流執行個體已關閉。The current stream instance is closed.

範例

這個程式碼範例是針對 MemoryStream 類別所提供的較大範例的一部分。This code example is part of a larger example provided for the MemoryStream class.

// 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)

備註

這個方法會覆寫 WriteThis method overrides Write.

offset 參數會提供 buffer 中要寫入之第一個位元組的位移,而 count 參數會提供要寫入的位元組數目。The offset parameter gives the offset of the first byte in buffer to write from, and the count parameter gives the number of bytes to write. 如果寫入作業成功,資料流程中目前的位置會依寫入的位元組數來前移。If the write operation is successful, the current position within the stream is advanced by the number of bytes written. 如果發生例外狀況,則資料流程中的目前位置不變。If an exception occurs, the current position within the stream is unchanged.

除了以 byte [] 參數所建立的 MemoryStream 之外,在 MemoryStream 結尾的寫入作業會展開 MemoryStreamExcept for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.

另請參閱

適用於