/// <summary> /// 检测端口是否被占用 /// </summary> /// <param name="port"></param> /// <returns></returns> public bool PortCheck(int port) { bool flag = false; IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();//获取所有的监听连接 foreach (IPEndPoint endPoint in ipEndPoints) { System.Diagnostics.Debug.WriteLine(endPoint.Port); if (endPoint.Port == port) { flag = true; } } return flag; }
也可以 netstat -a 查看当前活动端口
转载自 : http://www.cnblogs.com/bayonetxxx/archive/2009/07/22/1498747.html
时间: 2024-10-19 22:55:55