How to check who uses certain port in C#?

2019-05-13 18:39发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-05-13 19:24

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.

查看更多
登录 后发表回答