MemoryStream.Write 方法

定义

重载

名称 说明
Write(ReadOnlySpan<Byte>)

将包含在当前内存流中的 source 字节序列写入到当前内存流中,并按写入的字节数推进此内存流中的当前位置。

Write(Byte[], Int32, Int32)

使用从缓冲区读取的数据将字节块写入当前流。

Write(ReadOnlySpan<Byte>)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

将包含在当前内存流中的 source 字节序列写入到当前内存流中,并按写入的字节数推进此内存流中的当前位置。

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

参数

sourcebuffer
ReadOnlySpan<Byte>

内存区域。 此方法将该区域的内容复制到当前内存流。

适用于

Write(Byte[], Int32, Int32)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs

使用从缓冲区读取的数据将字节块写入当前流。

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

offset 参数提供要写入的第一个字节 buffer 的偏移量,参数 count 提供要写入的字节数。 如果写入操作成功,则流中的当前位置由写入的字节数进行高级。 如果发生异常,则流中的当前位置保持不变。

除使用 byte[] 参数构造的MemoryStream构造外,在展开的MemoryStream末尾MemoryStream写入操作。

另请参阅

适用于