Get PID of process using some port
11,401
Solution 1
This has been asked before, How do I get process name of an open port in C#?.
Solution 2
This is probably too late for the original poster but someone else may find it useful. You can use the PowerShell class in the System.Management.Automation namespace.
private static IEnumerable<uint> ProcessesUsingPorts(uint id)
{
PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-NetTCPConnection").AddParameter("LocalPort", id);
return ps.Invoke().Select(p => (uint)p.Properties["OwningProcess"].Value);
}
Author by
Zé Carlos
Updated on June 05, 2022Comments
-
Zé Carlos almost 2 years
I need a way of, given a tcp port number, discover if there's some process using that port (and get the process id).
Something like netstat does but programmatically.