Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
NamedPipeClientStream Class

Exposes a Stream around a named pipe, which supports both synchronous and asynchronous read and write operations.

Namespace:  System.IO.Pipes
Assembly:  System.Core (in System.Core.dll)
Visual Basic (Declaration)
<HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort := True)> _
Public NotInheritable Class NamedPipeClientStream _
    Inherits PipeStream
Visual Basic (Usage)
Dim instance As NamedPipeClientStream
C#
[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public sealed class NamedPipeClientStream : PipeStream
Visual C++
[HostProtectionAttribute(SecurityAction::LinkDemand, MayLeakOnAbort = true)]
public ref class NamedPipeClientStream sealed : public PipeStream
JScript
public final class NamedPipeClientStream extends PipeStream
NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Named pipes provide one-way or duplex pipes for communication between a pipe server and one or more pipe clients. Named pipes can be used for interprocess communication locally or over a network. A single pipe name can be shared by multiple NamedPipeClientStream objects.

Any process can act as either a named pipe server or client, or both.

Note   For Windows XP Professional and Windows 2000 Server, the maximum number of pipes that are permitted to simultaneously connect over the network is ten.

The following example demonstrates a way to send a string from a parent process to a child process on the same computer using named pipes. This example creates a NamedPipeServerStream object in a parent process. The NamedPipeServerStream object has a PipeDirection value of Out. The server then waits for a NamedPipeClientStream object in a child process to connect to it. In this example, both processes are on the same computer and the NamedPipeClientStream object has a PipeDirection value of In. The parent process then sends a user-supplied string to the child process. The string is displayed to the console.

This example is for the client process, which connects to the server process. For the entire code sample, including the code for both the pipe client and server, see How to: Use Named Pipes to Communicate Between Processes over a Network.

Visual Basic
Imports System
Imports System.IO
Imports System.IO.Pipes
Imports System.Security.Principal

Class PipeClient

    Shared Sub Main(ByVal args As String())

        Dim pipeClient As New NamedPipeClientStream("localhost", _
                    "testpipe", PipeDirection.In, PipeOptions.None)

        ' Connect to the pipe or wait until the pipe is available.
        Console.WriteLine("Attempting to connect to the pipe...")
        pipeClient.Connect()

        Console.WriteLine("Connect to the pipe.")
        Console.WriteLine("There are currently {0} pipe server instances open.", _
                          pipeClient.NumberOfServerInstances)

        Dim sr As New StreamReader(pipeClient)
        Dim temp As String

        temp = sr.ReadLine()
        While Not temp Is Nothing
            Console.WriteLine("Received from server: {0}", temp)
            temp = sr.ReadLine()
        End While
        Console.Write("Press Enter to continue...")
        Console.ReadLine()
    End Sub
End Class
C#
using System;
using System.IO;
using System.IO.Pipes;

class PipeClient
{
    static void Main(string[] args)
    {
        using (NamedPipeClientStream pipeClient =
            new NamedPipeClientStream(".", "testpipe", PipeDirection.In))
        {

            // Connect to the pipe or wait until the pipe is available.
            Console.Write("Attempting to connect to pipe...");
            pipeClient.Connect();

            Console.WriteLine("Connected to pipe.");
            Console.WriteLine("There are currently {0} pipe server instances open.",
               pipeClient.NumberOfServerInstances);
            using (StreamReader sr = new StreamReader(pipeClient))
            {
                // Display the read text to the console
                string temp;
                while ((temp = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Received from server: {0}", temp);
                }
            }
        }
        Console.Write("Press Enter to continue...");
        Console.ReadLine();
    }
}
System..::.Object
  System..::.MarshalByRefObject
    System.IO..::.Stream
      System.IO.Pipes..::.PipeStream
        System.IO.Pipes..::.NamedPipeClientStream
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
User permissions needed for named pipes in vista.      ksaunder   |   Edit   |   Show History
What i am trying to do (in Vista) is run a windows service that has a named piped server and an application that runs under limited permissions as a local user. When the local user app tries to connect to the server an error is thrown that ‘Access to the path is denied’. If the app is running under an administrator account it works perfectly. I need my app to be able to communicate with the service. I know there are other possible solutions but i felt that named pipes would be the most secure. Please any help is appreciated. the lanuage is c# using the namedPipeServerStream and namedPipeClientStream.
Tags What's this?: named (x) pipes (x) Add a tag
Flag as ContentBug
Similar permission issues in Vista      ahauptfleisch   |   Edit   |   Show History
I have the same issues as the post above. I read that one should modify the permissions through SetAccessControl. Tried that with a million different combinations, with the most logical looking something like this:


NamedPipeServerStream pipe = new NamedPipeServerStream("somename", PipeDirection.Out, 1, PipeTransmissionMode.Message, PipeOptions.None);

PipeAuditRule auditRule = new PipeAuditRule("Everyone", PipeAccessRights.FullControl, AuditFlags.Failure);
PipeAccessRule accessRule = new PipeAccessRule("Everyone", PipeAccessRights.FullControl, AccessControlType.Allow);

PipeSecurity security = pipe.GetAccessControl();

security.AddAuditRule(auditRule);
security.AddAccessRule(accessRule);

pipe.SetAccessControl(security);


The applications bombs out with an UnauthorizedAccessException on the final statement. I've also tried other system accounts and even executed this program with the "Run as Administrator" option, but no luck.

I'm sure the "Everyone" bit is correct. I've also tried it with a proper IdentityReference object, which I pulled from the AuthorizationRuleCollection obtained by running Pipe.GetAccessRules(...) .

This error occurs regardless of the settings given to the PipeAccessRights, AuditFlags and AccessControlType parameters.
Permission issue solved      ahauptfleisch   |   Edit   |   Show History
Managed to sort my problem out. Turns out that it isn't a permission problem, but the stream directions the pipe allows. Simply change the PipeDirection to InOut like this:


NamedPipeServerStream pipe = new NamedPipeServerStream("somename", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None);


I don't know why one would need to change it to InOut when you are only streaming data down, but I'm sure there's a logical explination.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker