using System; using System.Collections.Generic; using System.Net; using wrapGetExtendedTable; using System.Management; namespace TcpListenerList { class Program { static String getProcessImage(long pid) { String q = String.Format("Win32_Process.Handle={0}",pid); String ans = ""; try { ManagementObject mo = new ManagementObject(q); ans = mo.Properties["Caption"].Value.ToString() + " "; ans += mo.Properties["ExecutablePath"].Value.ToString() + " "; ans += mo.Properties["CommandLine"].Value.ToString(); } catch (Exception) { } return ans; } static void Main(string[] args) { iphlpapi aa = new iphlpapi(); MIB_TCPTABLE_OWNER_PID z = aa.xGetExtendedTcpTableOwnerPid(TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL); Console.WriteLine("{0,-24} {1,-21} {2,-21} {3,6} {4}", "STATE", "LocalAddress", "RemoteAddress", "PID", "Command & CommandLine"); for (int i = 0; i < z.table.Length; i++) { IPAddress a = new IPAddress(z.table[i].dwLocalAddr); IPAddress b = new IPAddress(z.table[i].dwRemoteAddr); Console.WriteLine("{0,-24} {1,15}:{2,5} {3,15}:{4,5} {5,6:#####0} {6}" , Enum.GetName(typeof(MIB_TCP_STATE), z.table[i].dwState) , a.ToString() , z.table[i].dwLocalPort , b.ToString() , z.table[i].dwRemotePort , z.table[i].dwOwningPid , getProcessImage((long)z.table[i].dwOwningPid)); } } } }