C# – Windows Process and Ip Address that it’s connected to

cnetnetwork-programmingwindows

I'm trying to get the ipaddress that a process is connected to in c#. Is there an easy way to do this?

Best Answer

There are two ways to achieve this:

  1. Use the undocumented InternalGetTcpTableWithOwnerModule / InternalGetUdpTableWithOwnerModule / InternalGetTcp6TableWithOwnerModule / InternalGetUdp6TableWithOwnerModule APIs exported from iphlpapi.dll.
  2. Run netstat -b and parse the output.

Either way you will need administrator privileges and both ways are bound to break with different Windows versions. It is most certainly possible to create a Windows driver with documented APIs, but that would be a lot of work.

Update:

There is actually a documented API too - GetExtendedTcpTable and GetExtendedUdpTable. Also there's an article which presents an example how to call it from C#.