private bool ISPortUsed(int port)
        {
            IList portUsed = PortUsing();
            return portUsed.Contains(port);
        }

        private IList PortUsing()
        {
            //获取本地计算机的网络连接和通信统计数据的信息 
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            //返回本地计算机上的所有Tcp监听程序 
            IPEndPoint[] ipsTCP = ipGlobalProperties.GetActiveTcpListeners();

            //返回本地计算机上的所有UDP监听程序 
            IPEndPoint[] ipsUDP = ipGlobalProperties.GetActiveUdpListeners();

            //返回本地计算机上的Internet协议版本4(IPV4 传输控制协议(TCP)连接的信息。 
            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();

            List<int> allPorts = 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;
        }

 全部源码下载:https://download.csdn.net/download/hanghangz/11250029

相关文章:

  • 2021-06-01
  • 2021-10-21
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-21
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
相关资源
相似解决方案