Beispiel #1
0
 private static void AddTCPNetPro(TcpRow item, List <NetProcess> proList)
 {
     try
     {
         var _netProcess = proList.FirstOrDefault(x => x.ProcessID == item.owningPid);
         if (_netProcess == null)
         {
             _netProcess = new NetProcess()
             {
                 ProcessID = item.owningPid, ProcessName = ProcessAPI.GetProcessNameByPID(item.owningPid)
             };
             proList.Add(_netProcess);
         }
         if (_netProcess.Ports.FirstOrDefault(x => x.Port == item.LocalPort) == null)
         {
             _netProcess.ProcessICon = ProcessAPI.GetIcon(item.owningPid, true);
         }
         _netProcess.Ports.Add(GetTcpProPort(item));
     }
     catch (Exception e)
     {
     }
 }
Beispiel #2
0
 private void AddUDPNetProcess(UdpRow item)
 {
     try
     {
         var _netProcess = netProcesses.FirstOrDefault(x => x.ProcessID == item.owningPid);
         if (_netProcess == null)
         {
             _netProcess = new NetProcess()
             {
                 ProcessID = item.owningPid, ProcessName = ProcessAPI.GetProcessNameByPID(item.owningPid)
             };
             netProcesses.Add(_netProcess);
         }
         if (_netProcess.Ports.FirstOrDefault(x => x.Port == item.LocalPort) == null)
         {
             _netProcess.ProcessICon = ProcessAPI.GetIcon(item.owningPid, true);
             _netProcess.Ports.Add(GetUDPProcessPort(item));
         }
     }
     catch (Exception e)
     {
     }
 }
Beispiel #3
0
 private static void AddTCPNetProcess(TcpRow item)
 {
     try
     {
         var _netProcess = netProcesses.FirstOrDefault(x => x.ProcessID == item.owningPid);
         if (_netProcess == null)
         {
             _netProcess = new NetProcess()
             {
                 ProcessID = item.owningPid
             };
             netProcesses.Add(_netProcess);
         }
         if (_netProcess.Ports.FirstOrDefault(x => x.Port == item.LocalPort) == null)
         {
             _netProcess.ProcessICon = ProcessAPI.GetIcon(item.owningPid, true);
         }
         _netProcess.Ports.Add(GetTcpProcessPort(item));
     }
     catch (Exception e)
     {
     }
 }
Beispiel #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;

            var pName = textBox1.Text;

            var lIp = textBox2.Text;

            var lPort = textBox3.Text;

            var rIp = textBox4.Text;

            var rPort = textBox5.Text;


            var tps = NetProcessAPI.GetAllTcpConnections();

            var ups = NetProcessAPI.GetAllUdpConnections();

            dataGridView1.Rows.Clear();

            foreach (var p in tps)
            {
                if (!string.IsNullOrEmpty(pName) && ProcessAPI.GetProcessNameByPID(p.owningPid).IndexOf(pName, StringComparison.OrdinalIgnoreCase) == -1)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(lIp) && p.LocalAddress.ToString() != lIp)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(lPort) && p.LocalPort.ToString() != lPort)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(rIp) && p.RemoteAddress.ToString() != rIp)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(rPort) && p.RemotePort.ToString() != rPort)
                {
                    continue;
                }

                dataGridView1.Rows.Add(new object[] { p.owningPid.ToString(), ProcessAPI.GetIcon(p.owningPid, true), ProcessAPI.GetProcessNameByPID(p.owningPid), "TCP", p.LocalAddress.ToString(), p.LocalPort.ToString(), p.RemoteAddress.ToString(), p.RemotePort.ToString() });
            }


            if (!string.IsNullOrEmpty(rIp) || !string.IsNullOrEmpty(rPort))
            {
                button1.Enabled = true;
                return;
            }

            foreach (var p in ups)
            {
                if (!string.IsNullOrEmpty(pName) && ProcessAPI.GetProcessNameByPID(p.owningPid).IndexOf(pName, StringComparison.OrdinalIgnoreCase) == -1)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(lIp) && p.LocalAddress.ToString() != lIp)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(lPort) && p.LocalPort.ToString() != lPort)
                {
                    continue;
                }

                dataGridView1.Rows.Add(new object[] { p.owningPid.ToString(), ProcessAPI.GetIcon(p.owningPid, true), ProcessAPI.GetProcessNameByPID(p.owningPid), "UDP", p.LocalAddress.ToString(), p.LocalPort.ToString(), "", "" });
            }

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow r = this.dataGridView1.Rows[i];
                r.HeaderCell.Value = string.Format("{0}", i + 1);
            }

            this.dataGridView1.Refresh();

            button1.Enabled = true;
        }
Beispiel #5
0
        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            //tcp
            if (this.comboBox1.SelectedText == "" || this.comboBox1.SelectedText == "全部" || this.comboBox1.SelectedText == "TCP")
            {
                var processNames = new List <string>();
                var allConns     = NetProcessAPI.GetAllTcpConnections();
                //pid
                if (!string.IsNullOrEmpty(this.textBox1.Text))
                {
                    var t = this.textBox1.Text.Split(',');
                    foreach (var p in t)
                    {
                        if (Int64.TryParse(p.Trim(), out long pid))
                        {
                            allConns = allConns.Where(x => x.owningPid == pid).ToArray();
                        }
                        else
                        {
                            processNames.Add(p.Trim());
                        }
                    }
                }
                //local port
                if (!string.IsNullOrEmpty(this.textBox2.Text))
                {
                    var t = this.textBox2.Text.Split(',');
                    foreach (var p in t)
                    {
                        if (Int32.TryParse(p.Trim(), out int port))
                        {
                            allConns = allConns.Where(x => x.LocalPort == port).ToArray();
                        }
                    }
                }
                //remote port
                if (!string.IsNullOrEmpty(this.textBox3.Text))
                {
                    var t = this.textBox3.Text.Split(',');
                    foreach (var p in t)
                    {
                        if (Int32.TryParse(p.Trim(), out int port))
                        {
                            allConns = allConns.Where(x => x.RemotePort == port).ToArray();
                        }
                    }
                }
                if (allConns.Any())
                {
                    foreach (var p in allConns)
                    {
                        var pname = ProcessAPI.GetProcessNameByPID(p.owningPid);
                        if (!processNames.Any() || processNames.Any(x => x.ToLower().Contains(pname.ToLower())))
                        {
                            var icon = ProcessAPI.GetIcon(p.owningPid, true);
                            dataGridView1.Rows.Add(new object[]
                            {
                                icon,
                                pname + " " + p.owningPid,
                                "TCP",
                                p.LocalAddress.ToString(),
                                p.LocalPort.ToString(),
                                p.RemoteAddress.ToString(),
                                p.RemotePort.ToString(),
                                p.state.ToString()
                            });
                        }
                    }
                }
            }


            //udp
            if (this.comboBox1.SelectedText == "" || this.comboBox1.SelectedText == "全部" || this.comboBox1.SelectedText == "UDP")
            {
                var allUconns    = NetProcessAPI.GetAllUdpConnections();
                var processNames = new List <string>();
                //pid
                if (!string.IsNullOrEmpty(this.textBox1.Text))
                {
                    var t = this.textBox1.Text.Split(',');
                    foreach (var p in t)
                    {
                        if (Int64.TryParse(p.Trim(), out long pid))
                        {
                            allUconns = allUconns.Where(x => x.owningPid == pid).ToArray();
                        }
                        else
                        {
                            processNames.Add(p.Trim());
                        }
                    }
                }
                //local port
                if (!string.IsNullOrEmpty(this.textBox2.Text))
                {
                    var t = this.textBox2.Text.Split(',');
                    foreach (var p in t)
                    {
                        if (Int32.TryParse(p.Trim(), out int port))
                        {
                            allUconns = allUconns.Where(x => x.LocalPort == port).ToArray();
                        }
                    }
                }
                if (allUconns != null)
                {
                    foreach (var p in allUconns)
                    {
                        var pname = ProcessAPI.GetProcessNameByPID(p.owningPid);
                        if (!processNames.Any() || processNames.Any(x => pname.ToLower().Contains(x.ToLower())))
                        {
                            var icon = ProcessAPI.GetIcon(p.owningPid, true);
                            dataGridView1.Rows.Add(new object[]
                            {
                                icon,
                                pname + " " + p.owningPid,
                                "UDP",
                                p.LocalAddress.ToString(),
                                p.LocalPort.ToString(),
                                "-",
                                "-",
                                "-"
                            });
                        }
                    }
                }
            }
        }