How to check who uses certain port in C#?

Deadly 提交于 2019-12-06 00:53:51

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!