BinaryWriter Class

Writes primitive types in binary to a stream and supports writing strings in a specific encoding.

Syntax

Declaration

[Serializable]
[ComVisible(true)]
public class BinaryWriter : IDisposable

Inheritance Hierarchy

Inherited Members

System.Object

, , , , , ,

Constructors summary

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

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

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

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

Fields summary

Specifies a BinaryWriter with no backing store.

Holds the underlying stream.

Properties summary

Gets the underlying stream of the BinaryWriter.

Methods summary

Releases all resources used by the current instance of the BinaryWriter class.

Releases the unmanaged resources used by the BinaryWriter and optionally releases the managed resources.

Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

Sets the position within the current stream.

Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.

Writes an unsigned byte to the current stream and advances the stream position by one byte.

Writes a byte array to the underlying stream.

Writes a region of a byte array to the current stream.

Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

Writes a character array to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

Writes a section of a character array to the current stream, and advances the current position of the stream in accordance with the Encoding used and perhaps the specific characters being written to the stream.

Writes a decimal value to the current stream and advances the stream position by sixteen bytes.

Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.

Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.

Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.

Writes an eight-byte signed integer to the current stream and advances the stream position by eight bytes.

Writes a signed byte to the current stream and advances the stream position by one byte.

Writes a four-byte floating-point value to the current stream and advances the stream position by four bytes.

Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.

Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.

Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.

Writes an eight-byte unsigned integer to the current stream and advances the stream position by eight bytes.

Writes a 32-bit integer in a compressed format.

Constructors

  • BinaryWriter()

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

    protected BinaryWriter()
  • BinaryWriter(Stream)

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

    public BinaryWriter(Stream output)

    Parameters

    • output

      The output stream.

    Exceptions

  • BinaryWriter(Stream, Encoding)

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

    public BinaryWriter(Stream output, Encoding encoding)

    Parameters

    • output

      The output stream.

    • encoding

      The character encoding to use.

    Exceptions

  • 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(Stream output, Encoding encoding, bool leaveOpen)

    Parameters

    • output

      The output stream.

    • encoding

      The character encoding to use.

    • leaveOpen

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

    Exceptions

Fields

  • Null

    Specifies a BinaryWriter with no backing store.

    public static readonly BinaryWriter Null
  • OutStream

    Holds the underlying stream.

    protected Stream OutStream

Properties

  • BaseStream

    Gets the underlying stream of the BinaryWriter.

    public virtual Stream BaseStream { get; }

    Property Value

    • The underlying stream associated with the BinaryWriter.

Methods

  • Dispose()

    Releases all resources used by the current instance of the BinaryWriter class.

    public void Dispose()
  • Dispose(Boolean)

    Releases the unmanaged resources used by the BinaryWriter and optionally releases the managed resources.

    protected virtual void Dispose(bool disposing)

    Parameters

    • disposing

      true to release both managed and unmanaged resources; false to release only unmanaged resources.

  • Flush()

    Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

    public virtual void Flush()
  • Seek(Int32, SeekOrigin)

    Sets the position within the current stream.

    public virtual long Seek(int offset, SeekOrigin origin)

    Parameters

    • offset

      A byte offset relative to origin.

    • origin

      A field of SeekOrigin indicating the reference point from which the new position is to be obtained.

    Returns

    • The position with the current stream.

    Exceptions

  • Write(Boolean)

    Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.

    public virtual void Write(bool value)

    Parameters

    • value

      The Boolean value to write (0 or 1).

    Exceptions

  • Write(Byte)

    Writes an unsigned byte to the current stream and advances the stream position by one byte.

    public virtual void Write(byte value)

    Parameters

    • value

      The unsigned byte to write.

    Exceptions

  • Write(Byte[])

    Writes a byte array to the underlying stream.

    public virtual void Write(byte[] buffer)

    Parameters

    • buffer

      A byte array containing the data to write.

    Exceptions

  • Write(Byte[], Int32, Int32)

    Writes a region of a byte array to the current stream.

    public virtual void Write(byte[] buffer, int index, int count)

    Parameters

    • buffer

      A byte array containing the data to write.

    • index

      The starting point in buffer at which to begin writing.

    • count

      The number of bytes to write.

    Exceptions

  • Write(Char)

    Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

    public virtual void Write(char ch)

    Parameters

    • ch

      The non-surrogate, Unicode character to write.

    Exceptions

  • Write(Char[])

    Writes a character array to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

    public virtual void Write(char[] chars)

    Parameters

    • chars

      A character array containing the data to write.

    Exceptions

  • Write(Char[], Int32, Int32)

    Writes a section of a character array to the current stream, and advances the current position of the stream in accordance with the Encoding used and perhaps the specific characters being written to the stream.

    public virtual void Write(char[] chars, int index, int count)

    Parameters

    • chars

      A character array containing the data to write.

    • index

      The starting point in chars from which to begin writing.

    • count

      The number of characters to write.

    Exceptions

  • Write(Decimal)

    Writes a decimal value to the current stream and advances the stream position by sixteen bytes.

    public virtual void Write(decimal value)

    Parameters

    • value

      The decimal value to write.

    Exceptions

  • Write(Double)

    Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.

    public virtual void Write(double value)

    Parameters

    • value

      The eight-byte floating-point value to write.

    Exceptions

  • Write(Int16)

    Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.

    public virtual void Write(short value)

    Parameters

    • value

      The two-byte signed integer to write.

    Exceptions

  • Write(Int32)

    Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.

    public virtual void Write(int value)

    Parameters

    • value

      The four-byte signed integer to write.

    Exceptions

  • Write(Int64)

    Writes an eight-byte signed integer to the current stream and advances the stream position by eight bytes.

    public virtual void Write(long value)

    Parameters

    • value

      The eight-byte signed integer to write.

    Exceptions

  • Write(SByte)

    Writes a signed byte to the current stream and advances the stream position by one byte.

    [CLSCompliant(false)]
    public virtual void Write(sbyte value)

    Parameters

    • value

      The signed byte to write.

    Exceptions

  • Write(Single)

    Writes a four-byte floating-point value to the current stream and advances the stream position by four bytes.

    public virtual void Write(float value)

    Parameters

    • value

      The four-byte floating-point value to write.

    Exceptions

  • Write(String)

    Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.

    public virtual void Write(string value)

    Parameters

    • value

      The value to write.

    Exceptions

  • Write(UInt16)

    Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.

    [CLSCompliant(false)]
    public virtual void Write(ushort value)

    Parameters

    • value

      The two-byte unsigned integer to write.

    Exceptions

  • Write(UInt32)

    Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.

    [CLSCompliant(false)]
    public virtual void Write(uint value)

    Parameters

    • value

      The four-byte unsigned integer to write.

    Exceptions

  • Write(UInt64)

    Writes an eight-byte unsigned integer to the current stream and advances the stream position by eight bytes.

    [CLSCompliant(false)]
    public virtual void Write(ulong value)

    Parameters

    • value

      The eight-byte unsigned integer to write.

    Exceptions

  • Write7BitEncodedInt(Int32)

    Writes a 32-bit integer in a compressed format.

    protected void Write7BitEncodedInt(int value)

    Parameters

    • value

      The 32-bit integer to be written.

    Exceptions

Details

Namespace

System.IO

Assembly

System.IO.dll

See Also