C# Find Local/Remote Addr base on process ID
I've used deviare API to help me find out the PIDs of the processes that are currently running on my desktop. Could anyone kindly guide me or enlighten me on how to get the local/remote IP address as well as their port numbers base on the PID I have? (What API should I make use of?)
I want something like this:
Public ArrayList GetRemoteAdder(int PID)
{
return remoteAddr;
}
I want to retrieve the remote IP Address that a process is accessing 开发者_JAVA百科base on the PID that i have.
Probably not the nicest way, but a quick one:
Call netstat -n -o
and parse the output. It might be a better idea to resort to Win32 magic to do that via P/Invoke, but your usecase (and the frequency of the call.. For a one-time thing it might be good enough) is not clear.
The signature doesn't make a lot of sense to me either: What would GetRemoteAdder() return? Every process can easily listen on multiple ports or open a bunch of outgoing connections, so a single string is probably not going to help.
精彩评论