Write Method

UnmanagedMemoryStream.Write Method (Byte[], Int32, Int32)


Writes a block of bytes to the current stream using data from a buffer.

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

public override void Write(
	byte[] buffer,
	int offset,
	int count


Type: System.Byte[]

The byte array from which to copy bytes to the current stream.

Type: System.Int32

The offset in the buffer at which to begin copying bytes to the current stream.

Type: System.Int32

The number of bytes to write to the current stream.

Exception Condition

The stream is closed.


The underlying memory does not support writing.

- or -

An attempt is made to write to the stream and the CanWrite property is false.

- or -

The count value is greater than the capacity of the stream.

- or -

The position is at the end of the stream capacity.


An I/O error occurs.


One of the specified parameters is less than zero.


The offset parameter minus the length of the buffer parameter is less than the count parameter.


The buffer parameter is null.

Writing occurs at the current position in the stream.

The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. A block of unmanaged memory is allocated and de-allocated using the Marshal class.

// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

unsafe class TestWriter

    static void Main()

            // Create some data to read and write.
            byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data.");

	    // Allocate a block of unmanaged memory and return an IntPtr object.	
            IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);

            // Get a byte pointer from the IntPtr object.
            byte* memBytePtr = (byte*) memIntPtr.ToPointer();

            // Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);

            // Write the data.
            writeStream.Write(message, 0, message.Length);

            // Close the stream.

            // Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);

	    // Create a byte array to hold data from unmanaged memory.
            byte[] outMessage = new byte[message.Length];

            // Read from unmanaged memory to the byte array.
            readStream.Read(outMessage, 0, message.Length);

            // Close the stream.

            // Display the data to the console.

            // Free the block of unmanaged memory.


Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft