MemoryStream.Write 方法

定義

多載

Write(ReadOnlySpan<Byte>)
Write(Byte[], Int32, Int32)

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

Write(ReadOnlySpan<Byte>)

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>

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.

參數會提供中buffer第一個位元組的位移來count寫入,而參數會提供要寫入的位元組數目。 offsetThe 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展開的MemoryStream結尾處寫入作業。Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.

另請參閱

適用於