Прочетете на английски Редактиране

Споделяне чрез


BinaryReader Constructors

Definition

Initializes a new instance of the BinaryReader class.

Overloads

BinaryReader(Stream)

Initializes a new instance of the BinaryReader class based on the specified stream and using UTF-8 encoding.

BinaryReader(Stream, Encoding)

Initializes a new instance of the BinaryReader class based on the specified stream and character encoding.

BinaryReader(Stream, Encoding, Boolean)

Initializes a new instance of the BinaryReader class based on the specified stream and character encoding, and optionally leaves the stream open.

BinaryReader(Stream)

Source:
BinaryReader.cs
Source:
BinaryReader.cs
Source:
BinaryReader.cs

Initializes a new instance of the BinaryReader class based on the specified stream and using UTF-8 encoding.

C#
public BinaryReader(System.IO.Stream input);

Parameters

input
Stream

The input stream.

Exceptions

The stream does not support reading, is null, or is already closed.

Examples

The following code example demonstrates how to store and retrieve application settings in a file.

C#
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);
        }
    }
}

Remarks

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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

BinaryReader(Stream, Encoding)

Source:
BinaryReader.cs
Source:
BinaryReader.cs
Source:
BinaryReader.cs

Initializes a new instance of the BinaryReader class based on the specified stream and character encoding.

C#
public BinaryReader(System.IO.Stream input, System.Text.Encoding encoding);

Parameters

input
Stream

The input stream.

encoding
Encoding

The character encoding to use.

Exceptions

The stream does not support reading, is null, or is already closed.

encoding is null.

Remarks

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.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

BinaryReader(Stream, Encoding, Boolean)

Source:
BinaryReader.cs
Source:
BinaryReader.cs
Source:
BinaryReader.cs

Initializes a new instance of the BinaryReader class based on the specified stream and character encoding, and optionally leaves the stream open.

C#
public BinaryReader(System.IO.Stream input, System.Text.Encoding encoding, bool leaveOpen);

Parameters

input
Stream

The input stream.

encoding
Encoding

The character encoding to use.

leaveOpen
Boolean

true to leave the stream open after the BinaryReader object is disposed; otherwise, false.

Exceptions

The stream does not support reading, is null, or is already closed.

encoding or input is null.

See also

Applies to

.NET 10 и други версии
Продукт Версии
.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, 10
.NET Framework 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