System.IO.BinaryWriter.Write 方法 (Byte[])

方法描述

将字节数组写入基础流。

语法定义(C# System.IO.BinaryWriter.Write 方法 (Byte[]) 的用法)

public virtual void Write(
	byte[] buffer
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
buffer System-Byte[] 包含要写入的数据的字节数组。
返回值 void

提示和注释

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.BinaryWriter.Write 方法 (Byte[])例子

下面的代码示例说明如何通过将内存用作备份来写入二进制数据,然后验证数据是否被正确写入。

using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        const int arrayLength = 1000;

        // Create random data to write to the stream.
        byte[] dataArray = new byte[arrayLength];
        new Random().NextBytes(dataArray);

        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());

        // Write the data to the stream.
        Console.WriteLine("Writing the data.");
        binWriter.Write(dataArray);

        // Create the reader using the stream from the writer.
        BinaryReader binReader = 
            new BinaryReader(binWriter.BaseStream);

        // Set Position to the beginning of the stream.
        binReader.BaseStream.Position = 0;

        // Read and verify the data.
        byte[] verifyArray = binReader.ReadBytes(arrayLength);
        if(verifyArray.Length != arrayLength)
        {
            Console.WriteLine("Error writing the data.");
            return;
        }
        for(int i = 0; i < arrayLength; i++)
        {
            if(verifyArray[i] != dataArray[i])
            {
                Console.WriteLine("Error writing the data.");
                return;
            }
        }
        Console.WriteLine("The data was written and verified.");
    }
}

异常

异常 异常描述
IOException 发生 I/O 错误。
ObjectDisposedException 流已关闭。
ArgumentNullException buffer 为 null。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。