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

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

virtual void Write(
	array<wchar_t>^ chars,
	int index,
	int count


Type: array<System::Char>^

A character array containing the data to write.

Type: System::Int32

The starting point in chars from which to begin writing.

Type: System::Int32

The number of characters to write.

Exception Condition

The buffer length minus index is less than count.


chars is null.


index or count is negative.


An I/O error occurs.


The stream is closed.

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

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

using namespace System;
using namespace System::IO;
int main()
   array<Char>^invalidPathChars = Path::InvalidPathChars;
   MemoryStream^ memStream = gcnew MemoryStream;
   BinaryWriter^ binWriter = gcnew 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 = gcnew 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);
   array<Char>^memoryData = gcnew array<Char>(arraySize);
   binReader->Read( memoryData, 0, arraySize );
   Console::WriteLine( memoryData );

Universal Windows Platform
Available since 8
.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