Esempio n. 1
0
        /// <summary>
        /// 读取/proc/net/dev文件(Linux)
        /// </summary>
        /// <returns></returns>
        private static NetIO ReadProcNetDev()
        {
            NetIO netIO = new NetIO();

            try
            {
                string output = "";

                var info = new ProcessStartInfo()
                {
                    FileName = "/bin/bash",
                    RedirectStandardOutput = true
                };
                info.Arguments = $"-c \"cat /proc/net/dev\"";

                using (var p = Process.Start(info))
                {
                    output = p.StandardOutput.ReadToEnd();
                }

                if (string.IsNullOrWhiteSpace(output))
                {
                    return(netIO);
                }

                long receive  = 0;
                long transmit = 0;
                var  lines    = output.Trim().Split('\n');
                for (int i = 2; i < lines.Length; i++)
                {
                    var arr = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    receive  += long.Parse(arr[1]);
                    transmit += long.Parse(arr[9]);
                }

                return(new NetIO()
                {
                    Receive = receive,
                    Transmit = transmit
                });
            }
            catch (Exception ex)
            {
                return(netIO);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 获取系统网络吞吐(Linux)
        /// </summary>
        /// <returns></returns>
        public static NetIO GetLinuxNetIO()
        {
            NetIO netIO = new NetIO();

            try
            {
                NetIO first = ReadProcNetDev();
                Thread.Sleep(1000);
                NetIO second = ReadProcNetDev();

                netIO.Receive  = second.Receive - first.Receive;
                netIO.Transmit = second.Transmit - first.Transmit;

                return(netIO);
            }
            catch (Exception ex)
            {
                return(netIO);
            }
        }