Private BOOL ISPortUsed ( int Port) { the IList portUsed = PortUsing (); return portUsed.Contains (Port); } Private the IList PortUsing () { // Get information network connection and communication statistics local computer IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties (); // return all the local computer Tcp listeners the IPEndPoint [] = ipsTCP ipGlobalProperties.GetActiveTcpListeners (); // return all the local computer UDP listener the IPEndPoint [] = ipsUDP ipGlobalProperties.GetActiveUdpListeners (); //Back Internet Protocol version on the local computer 4 (IPV4 Transmission Control Protocol (TCP) connection information. TcpConnectionInformation [] = tcpConnInfoArray ipGlobalProperties.GetActiveTcpConnections (); List < int > = Allports new new List < int > (); allPorts.AddRange ( ipsTCP.Select (n- => n.Port)); allPorts.AddRange (ipsUDP.Select (n- => n.Port)); allPorts.AddRange (tcpConnInfoArray.Select (n- => n.LocalEndPoint.Port)); return Allports; }
All source code Download: https://download.csdn.net/download/hanghangz/11250029