NamedPipeClientStream.NumberOfServerInstances 属性      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取共享同一管道名称的服务器实例的数量。
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 : intmember this.NumberOfServerInstances : int[<get: System.Security.SecurityCritical>]
member this.NumberOfServerInstances : intPublic ReadOnly Property NumberOfServerInstances As Integer属性值
共享同一管道名称的服务器实例的数量。
- 属性
例外
管道已中断或出现 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
注解
此属性返回当前NamedPipeClientStream对象具有句柄或连接到的 对象的服务器实例NamedPipeServerStream数。 如果当前 NamedPipeClientStream 对象尚未连接到命名管道服务器,或者如果尚未设置当前管道句柄,则此属性将 InvalidOperationException引发 。