C# 获取某个应用发送和接收流量

        private static void InitAdapter()
        {
            // Counter variables
            var counterLock = new object();
            int received = 0;
            int sent = 0;

            // Fetch ID of all BaiduNetdisk processes
            var processList = Process.GetProcessesByName("BaiduNetdisk").Select(p => p.Id).ToHashSet();

            GetProcessAndChildren(processList.First());

            // Run in another thread, since this will be a blocking operation (see below)
            Task.Run(() =>
            {
                // Start ETW session
                using (var session = new TraceEventSession("MyKernelAndClrEventsSession"))
                {
                    // Query network events
                    session.EnableKernelProvider(KernelTraceEventParser.Keywords.NetworkTCPIP);

                    // Subscribe to trace events
                    // These will be called by another thread, so locking is needed here
                    session.Source.Kernel.TcpIpRecv += data =>
                    {
                        if (list.Contains(data.ProcessID))
                            lock (counterLock)
                                received += data.size;
                    };

                    session.Source.Kernel.TcpIpSend += data =>
                    {
                        if (list.Contains(data.ProcessID))
                            lock (counterLock)
                                sent += data.size;
                    };

                    // Process all events (this will block)
                    session.Source.Process();
                }
            });

            // Program logic to handle counter values
            while (true)
            {
                // Wait some time and print current counter status
                Task.Delay(1000).Wait();
                lock (counterLock)
                    Console.WriteLine($"发送: {(sent / 1024.00).ToString("N")} kb/s    收到: {(received / 1024.00).ToString("N")} kb/s  总的:{((sent + received) / 1024.00).ToString("N")}");
            }

        }

        private static List<int> list = new List<int>();
        /**
 * 传入参数:父进程id
 * 功能:根据父进程id,获取相关的进程树
 */
        public static void GetProcessAndChildren(int pid)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
            ManagementObjectCollection moc = searcher.Get();
            foreach (ManagementObject mo in moc)
            {
                GetProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
            }
            try
            {
                Process proc = Process.GetProcessById(pid);
                Console.WriteLine(pid);
                list.Add(pid);
            }
            catch (ArgumentException)
            {
                /* process already exited */
            }
        }


本文作者:admin

本文链接:https://www.javalc.com/post/64.html

版权声明:本篇文章于2021-04-26,由admin发表,转载请注明出处:分享你我。如有疑问,请联系我们

C# 使用nodejs Socket.io4.x服务

发表评论

取消
扫码支持
登录
用户名
密码
注册
用户名
密码(至少8位)
确认密码
昵称
邮箱(请填写常用邮箱)
获取邀请码
邀请码
验证码
找回密码
用户名
邮箱
※ 重置链接将发送到邮箱