예제 #1
0
        private void GO()
        {
            ProcInfo = new ProcessPerformanceInfo()
            {
                ProcessID = 3684
            };

            int        pid   = ProcInfo.ProcessID;
            List <int> ports = new List <int>();

            #region get ports for certain process through executing the netstat -ano command  in cmd
            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

            //get ip address
            IPAddress[] addrList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
            string      IP       = addrList[0].ToString();


            //var devices = NpcapDeviceList.Instance;
            var devices = CaptureDeviceList.Instance;

            // differentiate based upon types

            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);
                }
            }
            while (true)
            {
                SetText1("proc NetTotalBytes : " + ProcInfo.NetTotalBytes);
                SetText2("proc NetSendBytes : " + ProcInfo.NetSendBytes);
                SetText3("proc NetRecvBytes : " + ProcInfo.NetRecvBytes);

                //Call refresh function every 1s
                RefreshInfo();
            }
        }