需要添加引用:system.dll
Dim t As String = "TCP" '协议
Dim port As Integer = 52177 '需要判断的端口
Dim flag As Boolean = False
Dim properties As System.Net.NetworkInformation.IPGlobalProperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
Dim ipendpoints() As System.Net.IPEndPoint
If t = "TCP" Then
ipendpoints = properties.GetActiveTcpListeners()
Else
ipendpoints = properties.GetActiveUdpListeners()
End If
For Each ipendpoint As System.Net.IPEndPoint In ipendpoints
If ipendpoint.Port = port Then
flag = True
Exit For
End If
Next
ipendpoints = Nothing
properties = Nothing
if flag then
msgbox( "端口使用中")
endif