static void StaticConn(bool Listen) { Process[] processes = Process.GetProcesses(); var Ipv4Tcpconnections = IPHelper.GetTcpConnections(IPVersion.IPv4, processes); int TcpTableRowCount = Ipv4Tcpconnections.Count; Console.WriteLine("Proto Local Address Foreign Address State PID USER Command"); for (int i = 0; i < TcpTableRowCount; i++) { TcpConnection current = Ipv4Tcpconnections[i]; if (Listen && current.State.ToString() != "Listening") { continue; } string Commands = GetCommandLine(current.ProcessId); string UserName = GetProcessUserName(current.ProcessId); string SourceAddr = string.Format("{0}:{1}", current.LocalAddress, current.LocalPort); string DestAddr = string.Format("{0}:{1}", current.RemoteAddress, current.RemotePort); string outputRow = string.Format("{0, -7}{1, -23}{2, -23}{3, -16}{4, -10} {5, -20} {6}", current.Protocol, SourceAddr, DestAddr, current.State, current.ProcessId, UserName, Commands); Console.WriteLine(outputRow); } var Ipv4Udpconnections = IPHelper.GetUdpConnections(IPVersion.IPv4, processes); int UdpTableRowCount = Ipv4Udpconnections.Count; for (int i = 0; i < UdpTableRowCount; i++) { UdpConnection current = Ipv4Udpconnections[i]; string Commands = GetCommandLine(current.ProcessId); string UserName = GetProcessUserName(current.ProcessId); string SourceAddr = string.Format("{0}:{1}", current.LocalAddress, current.LocalPort); string DestAddr = string.Format("{0}:{1}", current.RemoteAddress, current.RemotePort); string outputRow = string.Format("{0, -7}{1, -23}{2, -23}{3, -16}{4, -10} {5, -20} {6}", current.Protocol, SourceAddr, DestAddr, "", current.ProcessId, UserName, Commands); Console.WriteLine(outputRow); } }
static void MonitorConn(bool Listen) { Console.WriteLine("Proto Local Address Foreign Address State PID USER Command"); List <String> rowsList = new List <string>(); while (true) { Process[] processes = Process.GetProcesses(); var Ipv4Tcpconnections = IPHelper.GetTcpConnections(IPVersion.IPv4, processes); int TcpTableRowCount = Ipv4Tcpconnections.Count; //WindowsTop属性用于获取或设置控制台窗口区域相对于屏幕缓冲区的顶部位置。 Program.CurrentColumn = 0; int windowTop = Console.WindowTop; foreach (var current in Ipv4Tcpconnections) { if (Listen && current.State.ToString() != "Listening") { continue; } string Commands = GetCommandLine(current.ProcessId); string UserName = GetProcessUserName(current.ProcessId); string SourceAddr = string.Format("{0}:{1}", current.LocalAddress, current.LocalPort); string DestAddr = string.Format("{0}:{1}", current.RemoteAddress, current.RemotePort); string outputRow = string.Format("{0, -7}{1, -23}{2, -23}{3, -16}{4, -10} {5, -20} {6}", current.Protocol, SourceAddr, DestAddr, current.State, current.ProcessId, UserName, Commands); if (rowsList.Count < Program.CurrentColumn + 1) { Console.SetCursorPosition(0, Program.CurrentColumn + 1); Console.WriteLine("{0, -200}", outputRow); rowsList.Add(outputRow); } else if (rowsList[Program.CurrentColumn] != outputRow) { rowsList[Program.CurrentColumn] = outputRow; Console.SetCursorPosition(0, Program.CurrentColumn + 1); Console.WriteLine("{0, -200}", outputRow); } Program.CurrentColumn++; } var Ipv4Udpconnections = IPHelper.GetUdpConnections(IPVersion.IPv4, processes); foreach (var current in Ipv4Udpconnections) { string Commands = GetCommandLine(current.ProcessId); string UserName = GetProcessUserName(current.ProcessId); string SourceAddr = string.Format("{0}:{1}", current.LocalAddress, current.LocalPort); string DestAddr = string.Format("{0}:{1}", current.RemoteAddress, current.RemotePort); string outputRow = string.Format("{0, -7}{1, -23}{2, -23}{3, -16}{4, -10} {5, -20} {6}", current.Protocol, SourceAddr, DestAddr, "", current.ProcessId, UserName, Commands); if (rowsList.Count < Program.CurrentColumn + 1) { Console.SetCursorPosition(0, Program.CurrentColumn + 1); Console.WriteLine("{0, -200}", outputRow); rowsList.Add(outputRow); } else if (rowsList[Program.CurrentColumn] != outputRow) { rowsList[Program.CurrentColumn] = outputRow; Console.SetCursorPosition(0, Program.CurrentColumn + 1); Console.WriteLine("{0, -200}", outputRow); } Program.CurrentColumn++; } //Console.WriteLine("{0} - {1} - {2}", Program.BeforeColumn, Program.CurrentColumn, rowsList.Count); if (Program.BeforeColumn > Program.CurrentColumn) { int linesToBeCleared = Program.BeforeColumn - Program.CurrentColumn; rowsList.RemoveRange(Program.CurrentColumn, linesToBeCleared); for (int i = 1; i < linesToBeCleared + 2; i++) { Console.SetCursorPosition(0, Program.CurrentColumn + i); Console.WriteLine("{0, -200}", " "); } } Program.BeforeColumn = Program.CurrentColumn; Console.SetWindowPosition(0, windowTop); //in order to keep console scroll bar stay Thread.Sleep(100); } }