Pavel
Pavel

Reputation: 47

how to get list of opened sockets from some process?

Windows Resource Monitor have a nice network filter and its easy to get network statistics of some process (TCP connections, port listen, etc). I tried System.Diagnostics from this , but all examples are about CPU and Memory usage. I found this project, but it looks dirty and unsafe. The only i need is to get Resource Monitor functionality in my c# project to do something like

var process = new Process("ProcessName");
var sockets = process.getSockets();

Is there any safe and legal solution without byte shifting and pointers?

Upvotes: 2

Views: 5893

Answers (1)

selbie
selbie

Reputation: 104474

From the command line, netstat -a -n -o will get you this information - including process id for each socket open or connected. Admin privs with -b option will even get you the EXE name. So if worse comes to worse, you could spawn a process to run netstat get this info.

Programatically, the Win32 APIs that will get you this information include GetTcpTable2 and GetExtendedUdpTable . You can p/invoke from iphlpapi.dll to call these functions from .net code. See the IP helper reference for more info.

You'll need to write additional code to map the "process name" string that your code has referenced to process id number. (Or the reverse).

Upvotes: 4

Related Questions