开发者

Get PID of process using some port [duplicate]

开发者 https://www.devze.com 2023-03-13 20:18 出处:网络
This question already has answers here: 开发者_JAVA百科 How do I get process name of an open port in C#?
This question already has answers here: 开发者_JAVA百科 How do I get process name of an open port in C#? (4 answers) Closed 10 months ago.

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.


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消