BinaryWriter 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将二进制文件中的基元类型写入流,并支持以特定编码编写字符串。
public ref class BinaryWriter : IDisposablepublic ref class BinaryWriter : IAsyncDisposable, IDisposablepublic class BinaryWriter : IDisposablepublic class BinaryWriter : IAsyncDisposable, IDisposable[System.Serializable]
public class BinaryWriter : IDisposable[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class BinaryWriter : IDisposabletype BinaryWriter = class
    interface IDisposabletype BinaryWriter = class
    interface IAsyncDisposable
    interface IDisposable[<System.Serializable>]
type BinaryWriter = class
    interface IDisposable[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type BinaryWriter = class
    interface IDisposablePublic Class BinaryWriter
Implements IDisposablePublic Class BinaryWriter
Implements IAsyncDisposable, IDisposable- 继承
- 
				BinaryWriter
- 属性
- 实现
示例
下面的代码示例演示如何在文件中存储和检索应用程序设置。
using System;
using System.IO;
using System.Text;
class ConsoleApplication
{
    const string fileName = "AppSettings.dat";
    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }
    public static void WriteDefaultValues()
    {
        using (var stream = File.Open(fileName, FileMode.Create))
        {
            using (var writer = new BinaryWriter(stream, Encoding.UTF8, false))
            {
                writer.Write(1.250F);
                writer.Write(@"c:\Temp");
                writer.Write(10);
                writer.Write(true);
            }
        }
    }
    public static void DisplayValues()
    {
        float aspectRatio;
        string tempDirectory;
        int autoSaveTime;
        bool showStatusBar;
        if (File.Exists(fileName))
        {
            using (var stream = File.Open(fileName, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                {
                    aspectRatio = reader.ReadSingle();
                    tempDirectory = reader.ReadString();
                    autoSaveTime = reader.ReadInt32();
                    showStatusBar = reader.ReadBoolean();
                }
            }
            Console.WriteLine("Aspect ratio set to: " + aspectRatio);
            Console.WriteLine("Temp directory is: " + tempDirectory);
            Console.WriteLine("Auto save time set to: " + autoSaveTime);
            Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}
open System.IO
open System.Text
let fileName = "AppSettings.dat"
let writeDefaultValues () =
    use stream = File.Open(fileName, FileMode.Create)
    use writer = new BinaryWriter(stream, Encoding.UTF8, false)
    writer.Write 1.250F
    writer.Write @"c:\Temp"
    writer.Write 10
    writer.Write true
let displayValues () =
    if File.Exists fileName then
        use stream = File.Open(fileName, FileMode.Open)
        use reader = new BinaryReader(stream, Encoding.UTF8, false)
        let aspectRatio = reader.ReadSingle()
        let tempDirectory = reader.ReadString()
        let autoSaveTime = reader.ReadInt32()
        let showStatusBar = reader.ReadBoolean()
        printfn $"Aspect ratio set to: {aspectRatio}"
        printfn $"Temp directory is: {tempDirectory}"
        printfn $"Auto save time set to: {autoSaveTime}"
        printfn $"Show status bar: {showStatusBar}"
writeDefaultValues ()
displayValues ()
Imports System.IO
Module Module1
    Const fileName As String = "AppSettings.dat"
    Sub Main()
        WriteDefaultValues()
        DisplayValues()
    End Sub
    Sub WriteDefaultValues()
        Using writer As BinaryWriter = New BinaryWriter(File.Open(fileName, FileMode.Create))
            writer.Write(1.25F)
            writer.Write("c:\Temp")
            writer.Write(10)
            writer.Write(True)
        End Using
    End Sub
    Sub DisplayValues()
        Dim aspectRatio As Single
        Dim tempDirectory As String
        Dim autoSaveTime As Integer
        Dim showStatusBar As Boolean
        If (File.Exists(fileName)) Then
            Using reader As BinaryReader = New BinaryReader(File.Open(fileName, FileMode.Open))
                aspectRatio = reader.ReadSingle()
                tempDirectory = reader.ReadString()
                autoSaveTime = reader.ReadInt32()
                showStatusBar = reader.ReadBoolean()
            End Using
            Console.WriteLine("Aspect ratio set to: " & aspectRatio)
            Console.WriteLine("Temp directory is: " & tempDirectory)
            Console.WriteLine("Auto save time set to: " & autoSaveTime)
            Console.WriteLine("Show status bar: " & showStatusBar)
        End If
    End Sub
End Module
注解
BinaryWriter 类提供了简化将基元数据类型写入流的方法。 例如,可以使用 Write 方法将布尔值作为一字节值写入流。 该类包括支持不同数据类型的写入方法。
在创建 BinaryWriter 类的新实例时,请提供要写入的流,并根据需要指定编码类型,以及释放 BinaryWriter 对象后是否让流保持打开状态。 如果未指定编码类型,则使用 UTF-8。
重要
此类型实现 IDisposable 接口。 使用完该类型后,应直接或间接释放它。 若要直接释放类型,请在 try/catch 块中调用其 Dispose 方法。 若要间接处理它,请使用语言构造(如 using(在 C# 中)或 Using(在 Visual Basic 中)。 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”部分。
派生类可以重写此类的方法,以便提供唯一的字符编码。
构造函数
| BinaryWriter() | 初始化写入流 BinaryWriter 类的新实例。 | 
| BinaryWriter(Stream) | 根据指定的流并使用 UTF-8 编码初始化 BinaryWriter 类的新实例。 | 
| BinaryWriter(Stream, Encoding) | 根据指定的流和字符编码初始化 BinaryWriter 类的新实例。 | 
| BinaryWriter(Stream, Encoding, Boolean) | 根据指定的流和字符编码初始化 BinaryWriter 类的新实例,并选择性地将流保持打开状态。 | 
字段
| Null | 指定没有后盾存储的 BinaryWriter。 | 
| OutStream | 保存基础流。 | 
属性
| BaseStream | 获取 BinaryWriter的基础流。 | 
方法
| Close() | 关闭当前 BinaryWriter 和基础流。 | 
| Dispose() | 释放 BinaryWriter 类的当前实例使用的所有资源。 | 
| Dispose(Boolean) | 释放 BinaryWriter 使用的非托管资源,并选择性地释放托管资源。 | 
| DisposeAsync() | 异步释放 BinaryWriter 类的当前实例使用的所有资源。 | 
| Equals(Object) | 确定指定的对象是否等于当前对象。(继承自 Object) | 
| Flush() | 清除当前编写器的所有缓冲区,并导致任何缓冲数据写入基础设备。 | 
| GetHashCode() | 用作默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object的浅表副本。(继承自 Object) | 
| Seek(Int32, SeekOrigin) | 设置当前流中的位置。 | 
| ToString() | 返回一个表示当前对象的字符串。(继承自 Object) | 
| Write(Boolean) | 将一字节  | 
| Write(Byte) | 将无符号字节写入当前流,并将流位置提升一个字节。 | 
| Write(Byte[]) | 将字节数组写入基础流。 | 
| Write(Byte[], Int32, Int32) | 将字节数组的区域写入当前流。 | 
| Write(Char) | 将 Unicode 字符写入当前流,并根据所使用的  | 
| Write(Char[]) | 将字符数组写入当前流,并根据所使用的  | 
| Write(Char[], Int32, Int32) | 将字符数组的一部分写入当前流,并根据所使用的  | 
| Write(Decimal) | 将十进制值写入当前流,并将流位置提升 16 个字节。 | 
| Write(Double) | 将八字节浮点值写入当前流,并将流位置提升 8 个字节。 | 
| Write(Half) | 将双字节浮点值写入当前流,并将流位置提升两个字节。 | 
| Write(Int16) | 将双字节有符号整数写入当前流,并将流位置提升两个字节。 | 
| Write(Int32) | 将四字节有符号整数写入当前流,并将流位置提升 4 个字节。 | 
| Write(Int64) | 将八字节有符号整数写入当前流,并将流位置提升 8 个字节。 | 
| Write(ReadOnlySpan<Byte>) | 将字节范围写入当前流。 | 
| Write(ReadOnlySpan<Char>) | 将字符范围写入当前流,并根据使用的  | 
| Write(SByte) | 将有符号字节写入当前流,并将流位置向前推进一个字节。 | 
| Write(Single) | 将四字节浮点值写入当前流,并将流位置提升 4 个字节。 | 
| Write(String) | 在 BinaryWriter的当前编码中向此流写入长度前缀字符串,并根据使用的编码和写入流的特定字符推进流的当前位置。 | 
| Write(UInt16) | 将双字节无符号整数写入当前流,并将流位置提升两个字节。 | 
| Write(UInt32) | 将四字节无符号整数写入当前流,并将流位置提升 4 个字节。 | 
| Write(UInt64) | 将八字节无符号整数写入当前流,并将流位置提升 8 个字节。 | 
| Write7BitEncodedInt(Int32) | 以压缩格式写入 32 位整数。 | 
| Write7BitEncodedInt64(Int64) | 一次写出一个数字 7 位。 | 
显式接口实现
| IDisposable.Dispose() | 释放 BinaryWriter 使用的非托管资源,并选择性地释放托管资源。 | 
扩展方法
| ConfigureAwait(IAsyncDisposable, Boolean) | 配置如何执行从异步可释放项返回的任务的 await。 |