使用 .NET 访问类似 netstat 的数据

发布于 2024-08-21 12:50:58 字数 84 浏览 2 评论 0原文

我想知道是否有任何方法可以访问 .NET 框架内丢弃的数据包数量等信息。我知道 Win32_PerRawData 和 Ip Helper API。提前致谢

I'd like to know if there is any way to access info like number of discarded packets from within .NET framework. I am aware of Win32_PerRawData and Ip Helper API. Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的绿洲 2024-08-28 12:50:58

您可以使用 PerformanceCounter 类。运行 Perfmon.exe 以了解您的计算机上可用的内容。例如,您应该为每个网络适配器设置“网络接口 + 已丢弃的数据包”。

Your can use the PerformanceCounter class. Run Perfmon.exe to find out what's available on your machine. You should have Network Interface + Packets Received Discarded for each of your network adapters for example.

甜嗑 2024-08-28 12:50:58

这是懒惰和作弊的行为,但是...我知道我会因此而受到批评...您是否会考虑使用一个进程来执行 netstat -en 其中 n 是秒数间隔。如果您正在谈论 Winforms/WPF,使用 System.Diagnostics.Process 类将输出重定向到输入流(在其中可以解析丢弃的数据包)的隐藏窗口?

编辑:这是建议的代码示例

    public class TestNetStat
    {
        private StringBuilder sbRedirectedOutput = new StringBuilder();
        public string OutputData
        {
            get { return this.sbRedirectedOutput.ToString(); }
        }
        public void Run()
        {
            System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo();
            ps.FileName = "netstat";
            ps.ErrorDialog = false;
            ps.Arguments = "-e 30";   // Every 30 seconds
            ps.CreateNoWindow = true;
            ps.UseShellExecute = false;
            ps.RedirectStandardOutput = true;
            ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
            {
                proc.StartInfo = ps;
                proc.Exited += new EventHandler(proc_Exited);
                proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived);
                proc.Start();
                proc.WaitForExit();
                proc.BeginOutputReadLine();
                while (!proc.HasExited) ;
            }
        }

        void proc_Exited(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("proc_Exited: Process Ended");
        }

        void proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            if (e.Data != null) this.sbRedirectedOutput.Append(e.Data + Environment.NewLine);
            // Start parsing the sbRedirected for Discarded packets...
        }
    }

简单的隐藏窗口...

希望这有帮助,
此致,
汤姆.

This is lazy and cheating here but....I know I will get flamed for this...Would you not consider using a process to execute netstat -e n where n is the interval in number of seconds. If you are talking about a Winforms/WPF, using the System.Diagnostics.Process class to shell out to a hidden window with the output redirected to an input stream in which you can parse the discarded packets?

Edit: Here's a suggested code sample

    public class TestNetStat
    {
        private StringBuilder sbRedirectedOutput = new StringBuilder();
        public string OutputData
        {
            get { return this.sbRedirectedOutput.ToString(); }
        }
        public void Run()
        {
            System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo();
            ps.FileName = "netstat";
            ps.ErrorDialog = false;
            ps.Arguments = "-e 30";   // Every 30 seconds
            ps.CreateNoWindow = true;
            ps.UseShellExecute = false;
            ps.RedirectStandardOutput = true;
            ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
            {
                proc.StartInfo = ps;
                proc.Exited += new EventHandler(proc_Exited);
                proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived);
                proc.Start();
                proc.WaitForExit();
                proc.BeginOutputReadLine();
                while (!proc.HasExited) ;
            }
        }

        void proc_Exited(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("proc_Exited: Process Ended");
        }

        void proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            if (e.Data != null) this.sbRedirectedOutput.Append(e.Data + Environment.NewLine);
            // Start parsing the sbRedirected for Discarded packets...
        }
    }

Simple, hidden window....

Hope this helps,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文