BinaryWriter BinaryWriter BinaryWriter BinaryWriter Constructors

Definition

Overloads

BinaryWriter() BinaryWriter() BinaryWriter()

Initializes a new instance of the BinaryWriter class that writes to a stream.

BinaryWriter(Stream) BinaryWriter(Stream) BinaryWriter(Stream) BinaryWriter(Stream)

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

BinaryWriter(Stream, Encoding) BinaryWriter(Stream, Encoding) BinaryWriter(Stream, Encoding)

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

BinaryWriter(Stream, Encoding, Boolean) BinaryWriter(Stream, Encoding, Boolean) BinaryWriter(Stream, Encoding, Boolean)

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

BinaryWriter() BinaryWriter() BinaryWriter()

Initializes a new instance of the BinaryWriter class that writes to a stream.

protected:
 BinaryWriter();
protected BinaryWriter ();
Protected Sub New ()

Remarks

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

See also

BinaryWriter(Stream) BinaryWriter(Stream) BinaryWriter(Stream) BinaryWriter(Stream)

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

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

Parameters

output
Stream Stream Stream Stream

The output stream.

Exceptions

The stream does not support writing 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;

class ConsoleApplication
{
    const string fileName = "AppSettings.dat";

    static void Main()
    {
        WriteDefaultValues();
        DisplayValues();
    }

    public static void WriteDefaultValues()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            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 (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                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);
        }
    }
}
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

BinaryWriter(Stream, Encoding) BinaryWriter(Stream, Encoding) BinaryWriter(Stream, Encoding)

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

public:
 BinaryWriter(System::IO::Stream ^ output, System::Text::Encoding ^ encoding);
public BinaryWriter (System.IO.Stream output, System.Text.Encoding encoding);
new System.IO.BinaryWriter : System.IO.Stream * System.Text.Encoding -> System.IO.BinaryWriter

Parameters

output
Stream Stream Stream Stream

The output stream.

encoding
Encoding Encoding Encoding Encoding

The character encoding to use.

Exceptions

The stream does not support writing or is already closed.

Remarks

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

See also

BinaryWriter(Stream, Encoding, Boolean) BinaryWriter(Stream, Encoding, Boolean) BinaryWriter(Stream, Encoding, Boolean)

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

public:
 BinaryWriter(System::IO::Stream ^ output, System::Text::Encoding ^ encoding, bool leaveOpen);
public BinaryWriter (System.IO.Stream output, System.Text.Encoding encoding, bool leaveOpen);
new System.IO.BinaryWriter : System.IO.Stream * System.Text.Encoding * bool -> System.IO.BinaryWriter

Parameters

output
Stream Stream Stream Stream

The output stream.

encoding
Encoding Encoding Encoding Encoding

The character encoding to use.

leaveOpen
Boolean Boolean Boolean Boolean

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

Exceptions

The stream does not support writing or is already closed.

See also

Applies to