语言

NamedPipeClientStream.NumberOfServerInstances 属性

定义

获取共享同一管道名称的服务器实例数。

public:
 property int NumberOfServerInstances { int get(); };
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public int NumberOfServerInstances { get; }
public int NumberOfServerInstances { get; }
public int NumberOfServerInstances { [System.Security.SecurityCritical] get; }
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.NumberOfServerInstances : int
member this.NumberOfServerInstances : int
[<get: System.Security.SecurityCritical>]
member this.NumberOfServerInstances : int
Public ReadOnly Property NumberOfServerInstances As Integer

属性值

共享同一管道名称的服务器实例数。

特性

例外

尚未设置管道句柄。

-或-

当前 NamedPipeClientStream 对象尚未连接到对象 NamedPipeServerStream 。

管道损坏或出现 I/O 错误。

基础管道句柄已关闭。

示例

以下示例演示了使用命名管道将字符串从父进程发送到子进程的方法。 在此示例中,在子进程中创建一个 NamedPipeClientStream 对象,然后连接到本地计算机上的管道。 可以在类中看到 NamedPipeServerStream 服务器示例。 此示例是为 NamedPipeServerStream 这些和 NamedPipeClientStream 类提供的大型示例的一部分。

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();
    }
}
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

注解

此属性返回当前NamedPipeServerStream对象具有句柄或连接到的对象的服务器实例NamedPipeClientStream数。 如果当前 NamedPipeClientStream 对象尚未连接到命名管道服务器,或者当前管道句柄尚未设置,则此属性将引发一个 InvalidOperationException。

适用于