संपादित करें

इसके माध्यम से साझा किया गया


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.

public:
 BinaryReader(System::IO::Stream ^ input);
public BinaryReader (System.IO.Stream input);
new System.IO.BinaryReader : System.IO.Stream -> System.IO.BinaryReader
Public Sub New (input As Stream)

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.

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

Remarks

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

See also

Applies to

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.

public:
 BinaryReader(System::IO::Stream ^ input, System::Text::Encoding ^ encoding);
public BinaryReader (System.IO.Stream input, System.Text.Encoding encoding);
new System.IO.BinaryReader : System.IO.Stream * System.Text.Encoding -> System.IO.BinaryReader
Public Sub New (input As Stream, encoding As 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

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.

public:
 BinaryReader(System::IO::Stream ^ input, System::Text::Encoding ^ encoding, bool leaveOpen);
public BinaryReader (System.IO.Stream input, System.Text.Encoding encoding, bool leaveOpen);
new System.IO.BinaryReader : System.IO.Stream * System.Text.Encoding * bool -> System.IO.BinaryReader
Public Sub New (input As Stream, encoding As Encoding, leaveOpen As Boolean)

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