The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

BinaryWriter.Write Method (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.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

Public Overridable Sub Write (
	ch As Char


Type: System.Char

The non-surrogate, Unicode character to write.

Exception Condition

An I/O error occurs.


The stream is closed.


ch is a single surrogate character.

Because of data formatting conflicts, using this method with the following encodings is not recommended:

  • UTF-7

  • ISO-2022-JP


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

Unicode surrogate characters must be written out as pairs together in the same call, not individually. If you require support for surrogate pairs in your application, consider using a character array and the Write method overload.

The following code example shows how to read and write data using memory as a backing store.

Imports System
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()

        Dim i As Integer = 0
        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: ")
        For i = 0 To invalidPathChars.Length - 1
        Next i

        ' 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.
        Dim memoryData( _
            CInt(memStream.Length - memStream.Position) - 1) As Char
        For i = 0 To memoryData.Length - 1
            memoryData(i) = binReader.ReadChar()
        Next i

	End Sub
End Class

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft