2

I'm using the WMI management object searcher to find a process running on a machine and return the Process ID of that process.

What I need to do next is to find what port the process is listening on.

Is there a way to do this with WMI within C#? I came across this How to determine tcp port used by Windows process in C#, but it doesn't allow me to query based on PID.

I've had a good hour and a half Google session looking for a solution, help much appreciated at this stage!

Cheers Dave

Community
  • 1
  • 1
David C
  • 664
  • 1
  • 8
  • 21
  • Its not pretty, but as you have discovered, `netstat` will get you that info. You can capture the output from `netstat` and parse through it. This is a "quick n dirty" way of doing it and I would *never* use this kind of code in production. – Sam Axe Mar 14 '15 at 02:10
  • Yea that's unfortunate! I'm fairly new to .NET and rather liked how simple it was to pull information from the management object with SQL like queries! Cheers anyway. – David C Mar 14 '15 at 10:07
  • http://www.timvw.be/2007/09/09/build-your-own-netstatexe-with-c/ – Sam Axe Mar 14 '15 at 10:55

1 Answers1

1

The WMI doesn't include any class to query the list of TCP endpoints associated to a PID, To get that info you must interop with the GetExtendedTcpTable WinApi function.

RRUZ
  • 134,889
  • 20
  • 356
  • 483