Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检索进程网络使用情况_C#_.net_Networking_Network Programming_Performancecounter - Fatal编程技术网

C# 检索进程网络使用情况

C# 检索进程网络使用情况,c#,.net,networking,network-programming,performancecounter,C#,.net,Networking,Network Programming,Performancecounter,如何获取进程发送/接收字节?首选的方法是使用C# 我搜索了很多,没有找到任何简单的解决方法。一些解决方案建议在机器上安装WinPCap并使用此库 就像这家伙问的: 我不想要lib的开销 有没有简单的解决办法? 实际上,我需要Windows资源监视器在“具有网络活动的进程”选项卡下提供的确切数据: Windows的资源监视器如何获取此信息? 有什么例子吗 此外,尝试使用此处提到的计数器方法: 但是没有成功——因为不是每个过程都显示在这个计数器下面。 我再一次想知道,即使不使用此计数器,资源监视器如

如何获取进程发送/接收字节?首选的方法是使用C#

我搜索了很多,没有找到任何简单的解决方法。一些解决方案建议在机器上安装WinPCap并使用此库

就像这家伙问的: 我不想要lib的开销

有没有简单的解决办法? 实际上,我需要Windows资源监视器在“具有网络活动的进程”选项卡下提供的确切数据:

Windows的资源监视器如何获取此信息? 有什么例子吗

此外,尝试使用此处提到的计数器方法: 但是没有成功——因为不是每个过程都显示在这个计数器下面。 我再一次想知道,即使不使用此计数器,资源监视器如何获取此信息…

您可以使用。示例代码:

//Define 
string pn = "MyProcessName.exe";
var readOpSec  = new PerformanceCounter("Process","IO Read Operations/sec", pn);
var writeOpSec = new PerformanceCounter("Process","IO Write Operations/sec", pn);
var dataOpSec  = new PerformanceCounter("Process","IO Data Operations/sec", pn);
var readBytesSec = new PerformanceCounter("Process","IO Read Bytes/sec", pn);
var writeByteSec = new PerformanceCounter("Process","IO Write Bytes/sec", pn);
var dataBytesSec = new PerformanceCounter("Process","IO Data Bytes/sec", pn);

var counters = new List<PerformanceCounter>
                {
                readOpSec,
                writeOpSec,
                dataOpSec,
                readBytesSec,
                writeByteSec,
                dataBytesSec
                };

// get current value
foreach (PerformanceCounter counter in counters)
{
    float rawValue = counter.NextValue();

    // display the value
}
资源监视器使用-谢天谢地,微软已经创建了一个nice,使其更易于使用

我最近写了一些类似的东西来报告进程的网络IO:

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Diagnostics.Tracing.Parsers;
using Microsoft.Diagnostics.Tracing.Session;

namespace ProcessMonitoring
{
    public sealed class NetworkPerformanceReporter : IDisposable
    {
        private DateTime m_EtwStartTime;
        private TraceEventSession m_EtwSession;

        private readonly Counters m_Counters = new Counters();

        private class Counters
        {
            public long Received;
            public long Sent;
        }

        private NetworkPerformanceReporter() { }

        public static NetworkPerformanceReporter Create()
        {
            var networkPerformancePresenter = new NetworkPerformanceReporter();
            networkPerformancePresenter.Initialise();
            return networkPerformancePresenter;
        }

        private void Initialise()
        {
            // Note that the ETW class blocks processing messages, so should be run on a different thread if you want the application to remain responsive.
            Task.Run(() => StartEtwSession()); 
        }

        private void StartEtwSession()
        {
            try
            {
                var processId = Process.GetCurrentProcess().Id;
                ResetCounters();

                using (m_EtwSession = new TraceEventSession("MyKernelAndClrEventsSession"))
                {
                    m_EtwSession.EnableKernelProvider(KernelTraceEventParser.Keywords.NetworkTCPIP);

                    m_EtwSession.Source.Kernel.TcpIpRecv += data =>
                    {
                        if (data.ProcessID == processId)
                        {
                            lock (m_Counters)
                            {
                                m_Counters.Received += data.size;
                            }
                        }
                    };

                    m_EtwSession.Source.Kernel.TcpIpSend += data =>
                    {
                        if (data.ProcessID == processId)
                        {
                            lock (m_Counters)
                            {
                                m_Counters.Sent += data.size;
                            }
                        }
                    };

                    m_EtwSession.Source.Process();
                }
            }
            catch
            {
                ResetCounters(); // Stop reporting figures
                // Probably should log the exception
            }
        }

        public NetworkPerformanceData GetNetworkPerformanceData()
        {
            var timeDifferenceInSeconds = (DateTime.Now - m_EtwStartTime).TotalSeconds;

            NetworkPerformanceData networkData;

            lock (m_Counters)
            {
                networkData = new NetworkPerformanceData
                {
                    BytesReceived = Convert.ToInt64(m_Counters.Received / timeDifferenceInSeconds),
                    BytesSent = Convert.ToInt64(m_Counters.Sent / timeDifferenceInSeconds)
                };

            }

            // Reset the counters to get a fresh reading for next time this is called.
            ResetCounters();

            return networkData;
        }

        private void ResetCounters()
        {
            lock (m_Counters)
            {
                m_Counters.Sent = 0;
                m_Counters.Received = 0;
            }
            m_EtwStartTime = DateTime.Now;
        }

        public void Dispose()
        {
            m_EtwSession?.Dispose();
        }
    }

    public sealed class NetworkPerformanceData
    {
        public long BytesReceived { get; set; }
        public long BytesSent { get; set; }
    }
}
看一看这张照片。C#中有一个实现,它对每个进程传输的字节求和:


我很想知道这与

你们并没有在性能计数器中找到你们的答案吗?我尝试使用性能计数器的方式,但没有成功-甚至阅读了你添加的链接。已经阅读了-我不想使用任何第三方解决方案。没有其他本机方式?但我只需要网络活动,不需要设备I/o操作。“IO Read Operations/sec-显示进程发出读取I/O操作的速率(以每秒事件数为单位)。它统计进程生成的所有I/O活动,包括文件、网络和设备I/O。”没有其他本机方法(除非使用第三方LIB)来执行与单个进程相关的纯网卡IO。我所做的是监视进程IO和网卡IO。但后者并不特定于一个进程,即它收集与进程无关的所有IO计数器。我将编辑代码以显示网卡计数器。感谢您的解决方案,但这是我已经找到的解决方案之一,它无法实现我的目标,即仅获取进程网络使用率。我在寻找其他的方法,比如Windows的资源监视器获取这些信息……你不能将进程使用情况与系统使用情况进行比较吗?大概您可以获得接口信息来决定最大带宽是多少(10/100/等)。至少我认为这可能是我将如何处理它的。我问了一个简单的问题(几乎-stackoverflow认为它是一样的,错误地阻止了我,但我仍然不知道为什么结果是零,也没有在互联网上找到任何线索),但我有一个问题尚未解决,我得到的结果总是零。你遇到过和我一样的结果为零的问题吗?需要一些解决问题的指导方针(我还将app.config更改为帖子上的链接,用于安全性解除…还没有解决方案)。是否必须在管理员的领导下执行?我正在从
EnableKernelProvider
方法获取异常
系统。未经授权的访问异常
。我们是否可以在不使用管理员权限运行的情况下以某种方式获取结果?只有当前用户进程的详细信息才有效。
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Diagnostics.Tracing.Parsers;
using Microsoft.Diagnostics.Tracing.Session;

namespace ProcessMonitoring
{
    public sealed class NetworkPerformanceReporter : IDisposable
    {
        private DateTime m_EtwStartTime;
        private TraceEventSession m_EtwSession;

        private readonly Counters m_Counters = new Counters();

        private class Counters
        {
            public long Received;
            public long Sent;
        }

        private NetworkPerformanceReporter() { }

        public static NetworkPerformanceReporter Create()
        {
            var networkPerformancePresenter = new NetworkPerformanceReporter();
            networkPerformancePresenter.Initialise();
            return networkPerformancePresenter;
        }

        private void Initialise()
        {
            // Note that the ETW class blocks processing messages, so should be run on a different thread if you want the application to remain responsive.
            Task.Run(() => StartEtwSession()); 
        }

        private void StartEtwSession()
        {
            try
            {
                var processId = Process.GetCurrentProcess().Id;
                ResetCounters();

                using (m_EtwSession = new TraceEventSession("MyKernelAndClrEventsSession"))
                {
                    m_EtwSession.EnableKernelProvider(KernelTraceEventParser.Keywords.NetworkTCPIP);

                    m_EtwSession.Source.Kernel.TcpIpRecv += data =>
                    {
                        if (data.ProcessID == processId)
                        {
                            lock (m_Counters)
                            {
                                m_Counters.Received += data.size;
                            }
                        }
                    };

                    m_EtwSession.Source.Kernel.TcpIpSend += data =>
                    {
                        if (data.ProcessID == processId)
                        {
                            lock (m_Counters)
                            {
                                m_Counters.Sent += data.size;
                            }
                        }
                    };

                    m_EtwSession.Source.Process();
                }
            }
            catch
            {
                ResetCounters(); // Stop reporting figures
                // Probably should log the exception
            }
        }

        public NetworkPerformanceData GetNetworkPerformanceData()
        {
            var timeDifferenceInSeconds = (DateTime.Now - m_EtwStartTime).TotalSeconds;

            NetworkPerformanceData networkData;

            lock (m_Counters)
            {
                networkData = new NetworkPerformanceData
                {
                    BytesReceived = Convert.ToInt64(m_Counters.Received / timeDifferenceInSeconds),
                    BytesSent = Convert.ToInt64(m_Counters.Sent / timeDifferenceInSeconds)
                };

            }

            // Reset the counters to get a fresh reading for next time this is called.
            ResetCounters();

            return networkData;
        }

        private void ResetCounters()
        {
            lock (m_Counters)
            {
                m_Counters.Sent = 0;
                m_Counters.Received = 0;
            }
            m_EtwStartTime = DateTime.Now;
        }

        public void Dispose()
        {
            m_EtwSession?.Dispose();
        }
    }

    public sealed class NetworkPerformanceData
    {
        public long BytesReceived { get; set; }
        public long BytesSent { get; set; }
    }
}