使用英语阅读

通过


MemoryStream.Write 方法

定义

重载

Write(ReadOnlySpan<Byte>)

source 中包含的字节序列写入当前内存流,并按写入的字节数向前移动内存流的当前位置。

Write(Byte[], Int32, Int32)

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

Write(ReadOnlySpan<Byte>)

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

source 中包含的字节序列写入当前内存流,并按写入的字节数向前移动内存流的当前位置。

public override void Write (ReadOnlySpan<byte> source);
public override void Write (ReadOnlySpan<byte> buffer);

参数

sourcebuffer
ReadOnlySpan<Byte>

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

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Write(Byte[], Int32, Int32)

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

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

public override void Write (byte[] buffer, int offset, int count);

参数

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

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

除了 MemoryStream 使用 byte[] 参数构造的 之外,在 末尾的 MemoryStream 写入操作会扩展 MemoryStream

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0