Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

BinaryWriter.Write Method

Writes a value to the current stream.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Boolean)
[C#] public virtual void Write(bool);
[C++] public: virtual void Write(bool);
[JScript] public function Write(Boolean);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Byte)
[C#] public virtual void Write(byte);
[C++] public: virtual void Write(unsigned char);
[JScript] public function Write(Byte);

Writes a byte array to the underlying stream.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Byte())
[C#] public virtual void Write(byte[]);
[C++] public: virtual void Write(unsigned char __gc[]);
[JScript] public function Write(Byte[]);

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.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Char)
[C#] public virtual void Write(char);
[C++] public: virtual void Write(__wchar_t);
[JScript] public function 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.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Char())
[C#] public virtual void Write(char[]);
[C++] public: virtual void Write(__wchar_t __gc[]);
[JScript] public function Write(Char[]);

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

[Visual Basic] Overloads Public Overridable Sub Write(Decimal)
[C#] public virtual void Write(decimal);
[C++] public: virtual void Write(Decimal);
[JScript] public function Write(Decimal);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Double)
[C#] public virtual void Write(double);
[C++] public: virtual void Write(double);
[JScript] public function Write(double);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Short)
[C#] public virtual void Write(short);
[C++] public: virtual void Write(short);
[JScript] public function Write(Int16);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Integer)
[C#] public virtual void Write(int);
[C++] public: virtual void Write(int);
[JScript] public function Write(int);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Long)
[C#] public virtual void Write(long);
[C++] public: virtual void Write(__int64);
[JScript] public function Write(long);

Writes a signed byte to the current stream and advances the stream position by one byte. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(SByte)
[C#] public virtual void Write(sbyte);
[C++] public: virtual void Write(char);
[JScript] public function Write(SByte);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Single)
[C#] public virtual void Write(float);
[C++] public: virtual void Write(float);
[JScript] public function Write(float);

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.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(String)
[C#] public virtual void Write(string);
[C++] public: virtual void Write(String*);
[JScript] public function Write(String);

Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(UInt16)
[C#] public virtual void Write(ushort);
[C++] public: virtual void Write(unsigned short);
[JScript] public function Write(UInt16);

Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(UInt32)
[C#] public virtual void Write(uint);
[C++] public: virtual void Write(unsigned int);
[JScript] public function Write(UInt32);

Writes an eight-byte unsigned integer to the current stream and advances the stream position by eight bytes. This method is not CLS-compliant.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(UInt64)
[C#] public virtual void Write(ulong);
[C++] public: virtual void Write(unsigned __int64);
[JScript] public function Write(UInt64);

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Byte(), Integer, Integer)
[C#] public virtual void Write(byte[], int, int);
[C++] public: virtual void Write(unsigned char __gc[], int, int);
[JScript] public function Write(Byte[], int, int);

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.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Write(Char(), Integer, Integer)
[C#] public virtual void Write(char[], int, int);
[C++] public: virtual void Write(__wchar_t __gc[], int, int);
[JScript] public function Write(Char[], int, int);

Example

[Visual Basic, C#, C++] The following code example shows how to read and write data using memory as a backing store.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Write. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        Dim invalidPathChars() As Char = Path.InvalidPathChars
        Dim memStream As new MemoryStream()
        Dim binWriter As New BinaryWriter(memStream)

        ' Write to memory.
        binWriter.Write("Invalid file path characters are: ")
        binWriter.Write(Path.InvalidPathChars, 0, _
            Path.InvalidPathChars.Length)

        ' Create the reader using the same MemoryStream 
        ' as used with the writer.
        Dim binReader As New BinaryReader(memStream)

        ' Set Position to the beginning of the stream.
        memStream.Position = 0

        ' Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString())
        Dim upperBound As Integer = _
            CInt(memStream.Length - memStream.Position) - 1
        Dim memoryData(upperBound) As Char
        binReader.Read(memoryData, 0, upperBound)
        Console.WriteLine(memoryData)
    
    End Sub
End Class

[C#] 
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        char[] invalidPathChars = Path.InvalidPathChars;
        MemoryStream memStream = new MemoryStream();
        BinaryWriter binWriter = new BinaryWriter(memStream);

        // Write to memory.
        binWriter.Write("Invalid file path characters are: ");
        binWriter.Write(
            Path.InvalidPathChars, 0, Path.InvalidPathChars.Length);

        // Create the reader using the same MemoryStream 
        // as used with the writer.
        BinaryReader binReader = new BinaryReader(memStream);

        // Set Position to the beginning of the stream.
        memStream.Position = 0;

        // Read the data from memory and write it to the console.
        Console.Write(binReader.ReadString());
        int arraySize = (int)(memStream.Length - memStream.Position);
        char[] memoryData = new char[arraySize];
        binReader.Read(memoryData, 0, arraySize);
        Console.WriteLine(memoryData);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

void main()
{
    Char invalidPathChars __gc[] = Path::InvalidPathChars;
    MemoryStream* memStream = new MemoryStream();
    BinaryWriter* binWriter = new BinaryWriter(memStream);

    // Write to memory.
    binWriter->Write(S"Invalid file path characters are: ");
    binWriter->Write(
        Path::InvalidPathChars, 0, Path::InvalidPathChars->Length);

    // Create the reader using the same MemoryStream 
    // as used with the writer.
    BinaryReader* binReader = new BinaryReader(memStream);

    // Set Position to the beginning of the stream.
    binReader->BaseStream->Position = 0;

    // Read the data from memory and write it to the console.
    Console::Write(binReader->ReadString());
    int arraySize = (int)(memStream->Length - memStream->Position);
    Char memoryData __gc[] = new Char __gc[arraySize];
    binReader->Read(memoryData, 0, arraySize);
    Console::WriteLine(memoryData);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

BinaryWriter Class | BinaryWriter Members | System.IO Namespace

Show:
© 2015 Microsoft