Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Read Method

PipeStream.Read Method

Reads a block of bytes from a stream and writes the data to a specified buffer.

Namespace:  System.IO.Pipes
Assembly:  System.Core (in System.Core.dll)

public override int Read(
	byte[] buffer,
	int offset,
	int count


Type: System.Byte[]

When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

Type: System.Int32

The byte offset in the buffer array at which the bytes that are read will be placed.

Type: System.Int32

The maximum number of bytes to read.

Return Value

Type: System.Int32
The total number of bytes that are read into buffer. This might be less than the number of bytes requested if that number of bytes is not currently available, or 0 if the end of the stream is reached.


buffer is null.


offset is less than 0.


count is less than 0.


count is greater than the number of bytes available in buffer.


The pipe is closed.


The pipe does not support read operations.


The pipe is disconnected, waiting to connect, or the handle has not been set.


Any I/O error occurred.

Use the CanRead property to determine whether the current PipeStream object supports read operations.

Calling the Read method blocks until count bytes are read or the end of the stream is reached. For asynchronous read operations, see BeginRead and EndRead.

The following example creates an anonymous pipe client and pipe server. The pipe server uses the Read method to read a series of bytes from the pipe client as a validation code. Both the pipe client and the pipe server are part of the same example. The server portion of the example creates a client process and passes it an anonymous pipe handle as an argument.

using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeStreamExample
    private static byte[] matchSign = {9, 0, 9, 0};

    public static void Main()
        string[] args = Environment.GetCommandLineArgs();
        if (args.Length < 2)
            Process clientProcess = new Process();

            clientProcess.StartInfo.FileName = Environment.CommandLine;

            using (AnonymousPipeServerStream pipeServer =
                new AnonymousPipeServerStream(PipeDirection.In,
                // Pass the client process a handle to the server.
                clientProcess.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
                clientProcess.StartInfo.UseShellExecute = false;
                Console.WriteLine("[SERVER] Starting client process...");


                    if (WaitForClientSign(pipeServer))
                        Console.WriteLine("[SERVER] Valid sign code received!");
                        Console.WriteLine("[SERVER] Invalid sign code received!");
                catch (IOException e)
                    Console.WriteLine("[SERVER] Error: {0}", e.Message);
            Console.WriteLine("[SERVER] Client quit. Server terminating.");
            using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.Out, args[1]))
                    Console.WriteLine("[CLIENT] Sending sign code...");
                catch (IOException e)
                     Console.WriteLine("[CLIENT] Error: {0}", e.Message);
            Console.WriteLine("[CLIENT] Terminating.");

    private static bool WaitForClientSign(PipeStream inStream)
        byte[] inSign = new byte[matchSign.Length];
        int len = inStream.Read(inSign, 0, matchSign.Length);
        bool valid = len == matchSign.Length;

        while (valid && len-- > 0)
            valid = valid && (matchSign[len] == inSign[len]);
        return valid;

    private static void SendClientSign(PipeStream outStream)
        outStream.Write(matchSign, 0, matchSign.Length);

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft