Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check who uses certain port in C#?

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.

like image 397
Louis Rhys Avatar asked Dec 16 '25 16:12

Louis Rhys


1 Answers

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.

like image 52
Avner Shahar-Kashtan Avatar answered Dec 19 '25 05:12

Avner Shahar-Kashtan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!