问题
How to determine what process/what executable is using, say, port 80 of my localhost? I would like to know, for example if it is Apache Server, etc.
Can we get some information from ipProperties.GetActiveTcpListeners() ? I've only seen local endpoint, remote endpoint and state.
回答1:
You can pipe the output of netstat -o
and parse it, but that's probably a terrible idea full of headaches and edge cases.
Behind the scenes, netstat -o
uses the GetTcpTable2 API method from the IPHelper library, which returns a MIB_TCPTABLE2 structure, with each port represented by a MIB_TCPROW2 structure. You'll have to use P/Invoke to access this from C#, building interop structs for the table and the row
Checking PInvoke.Net, I see that a similar API call has already been mapped to C# - GetExtendedTcpTable - which lists available TCP ports for an app. You can use that as a base for building your interop structs and declarations.
来源:https://stackoverflow.com/questions/9110211/how-to-check-who-uses-certain-port-in-c