示例#1
0
        public void calcute(Process[] proc)
        {
            int k = 0;

            foreach (var item in proc)
            {
                //  lvwFile.Items.Clear();
                var item2 = new ListViewItem(item.ProcessName);
                item2.SubItems.Add(item.Id.ToString());
                ProcInfo.ProcessID = item.Id;
                int        pid   = ProcInfo.ProcessID;
                List <int> ports = new List <int>();
                #region 获取指定进程对应端口号
                Process pro = new Process();
                pro.StartInfo.FileName               = "cmd.exe";
                pro.StartInfo.UseShellExecute        = false;
                pro.StartInfo.RedirectStandardInput  = true;
                pro.StartInfo.RedirectStandardOutput = true;
                pro.StartInfo.RedirectStandardError  = true;
                pro.StartInfo.CreateNoWindow         = true;
                pro.Start();
                pro.StandardInput.WriteLine("netstat -ano");
                pro.StandardInput.WriteLine("exit");
                Regex  reg  = new Regex("\\s+", RegexOptions.Compiled);
                string line = null;
                ports.Clear();

                while ((line = pro.StandardOutput.ReadLine()) != null)
                {
                    line = line.Trim();
                    if (line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase))
                    {
                        line = reg.Replace(line, ",");
                        string[] arr = line.Split(',');
                        if (arr[4] == pid.ToString())
                        {
                            string soc = arr[1];
                            int    pos = soc.LastIndexOf(':');
                            int    pot = int.Parse(soc.Substring(pos + 1));
                            ports.Add(pot);
                        }
                    }
                    else if (line.StartsWith("UDP", StringComparison.OrdinalIgnoreCase))
                    {
                        line = reg.Replace(line, ",");
                        string[] arr = line.Split(',');
                        if (arr[3] == pid.ToString())
                        {
                            string soc = arr[1];
                            int    pos = soc.LastIndexOf(':');
                            int    pot = int.Parse(soc.Substring(pos + 1));
                            ports.Add(pot);
                        }
                    }
                }
                pro.Close();
                #endregion
                IPAddress[] addrList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
                string      IP       = addrList[0].ToString();
                //获取本机网络设备
                var devices = CaptureDeviceList.Instance;
                int count   = devices.Count;
                if (count < 1)
                {
                    Console.WriteLine("No device found on this machine");
                    return;
                }
                for (int i = 0; i < count; ++i)
                {
                    for (int j = 0; j < ports.Count; ++j)
                    {
                        CaptureFlowRecv(IP, ports[j], i);
                        CaptureFlowSend(IP, ports[j], i);
                    }
                }

                Console.WriteLine(k.ToString() + " " + ProcInfo.ProcessID + "proc NetSendBytes : " + ProcInfo.NetSendBytes);
                Console.WriteLine(k.ToString() + " " + ProcInfo.ProcessID + "proc NetRecvBytes : " + ProcInfo.NetRecvBytes);
                k++;



                item2.SubItems.Add(ProcInfo.NetSendBytes.ToString());
                item2.SubItems.Add(ProcInfo.NetRecvBytes.ToString());
                this.lvwFile.Items.Add(item2);
                ProcInfo.NetRecvBytes = 0;
                ProcInfo.NetSendBytes = 0;
                //每隔1s调用刷新函数对性能参数进行刷新
                //  RefershInfo();

                //最后要记得调用Dispose方法停止抓包并关闭设备
                ProcInfo.Dispose();
            }
        }
示例#2
0
 private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
 {
     ProcInfo.Dispose();
     t.Abort();
 }