/* * 由SharpDevelop创建。 * 用户: yedong_weiphone * 日期: 2015/10/23 * 时间: 17:15 * * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件 */ using System; using System.Diagnostics; using System.Text; namespace repl.Tools.NetworkTraffic { class Program { public static void Main(string[] args) { if (args.Length <= 0) {return;} const string IPV4 = "IPv4"; const string IPV6 = "IPv6"; string interface4 = ""; string interface6 = ""; decimal rc = 0; decimal recvVal4 = 0; string unitR4 = " KB"; decimal sendVal4 = 0; string unitS4 = " KB"; decimal recvVal6 = 0; string unitR6 = " KB"; decimal sendVal6 = 0; string unitS6 = " KB"; decimal recvValB = 0; string unitRB = " KB"; decimal sendValB = 0; string unitSB = " KB"; string returnValue = ""; string[]arr = null; interface4 = args[0]; if (args.Length >= 2) {interface6 = args[1];} if (interface4 != null && interface4 != "") { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.FileName = "cmd"; startInfo.Arguments = " /c \"for /f \"tokens=3,4\" %a in ('netsh int " + IPV4 + " show subint " + "\"" + interface4 + "\" ^| find /i " + "\"" + interface4 + "\"') do @echo %a %b\""; Process proc = new Process(); proc.StartInfo = startInfo; proc.Start(); returnValue = proc.StandardOutput.ReadToEnd(); if (returnValue != null && returnValue != "") { arr = returnValue.Split(' '); returnValue = null; rc = decimal.Parse(arr[0]); recvValB = rc; if (rc < (1024 * 1024)) { recvVal4 = Math.Round(rc / 1024, 2); unitR4 = " KB"; } else { recvVal4 = Math.Round(rc / 1024 / 1024, 2); unitR4 = " MB"; } rc = 0; rc = decimal.Parse(arr[1]); sendValB = rc; if (rc < (1024 * 1024)) { sendVal4 = Math.Round(rc / 1024, 2); unitS4 = " KB"; } else { sendVal4 = Math.Round(rc / 1024 / 1024, 2); unitS4 = " MB"; } rc = 0; } } if (interface6 != null && interface6 != "") { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.FileName = "cmd"; startInfo.Arguments = " /c \"for /f \"tokens=3,4\" %a in ('netsh int " + IPV6 + " show subint " + "\"" + interface6 + "\" ^| find /i " + "\"" + interface6 + "\"') do @echo %a %b\""; Process proc = new Process(); proc.StartInfo = startInfo; proc.Start(); returnValue = proc.StandardOutput.ReadToEnd(); if (returnValue != null && returnValue != "") { arr = returnValue.Split(' '); returnValue = null; rc = decimal.Parse(arr[0]); recvValB += rc; if (rc < (1024 * 1024)) { recvVal6 = Math.Round(rc / 1024, 2); unitR6 = " KB"; } else { recvVal6 = Math.Round(rc / 1024 / 1024, 2); unitR6 = " MB"; } rc = 0; rc = decimal.Parse(arr[1]); sendValB += rc; if (rc < (1024 * 1024)) { sendVal6 = Math.Round(rc / 1024, 2); unitS6 = " KB"; } else { sendVal6 = Math.Round(rc / 1024 / 1024, 2); unitS6 = " MB"; } } } if (interface4 == null || interface4 == "") {interface4 = "N/A";} if (interface6 == null || interface6 == "") {interface6 = "N/A";} if (recvValB < (1024 * 1024)) { recvValB = Math.Round(recvValB / 1024, 2); unitRB = " KB"; } else { recvValB = Math.Round(recvValB / 1024 / 1024, 2); unitRB = " MB"; } if (sendValB < (1024 * 1024)) { sendValB = Math.Round(sendValB / 1024, 2); unitSB = " KB"; } else { sendValB = Math.Round(sendValB / 1024 / 1024, 2); unitSB = " MB"; } StringBuilder sb = new StringBuilder(); sb.AppendLine("NetworkTraffic.exe 获取当前指定网络接口的流量数据。"); sb.AppendLine("Copyright (C) 2012 repl"); sb.AppendLine(); sb.AppendLine("用法: NetworkTraffic.exe\t\"IPv4 接口名\" \"IPv6 接口名\""); sb.AppendLine("\t\t\t\t如果无某一个接口名, 则用 \"\" 代替"); sb.AppendLine(); sb.AppendLine("Interface: " + interface4 + " Type: " + IPV4 + " Recv: " + recvVal4 + unitR4 + " Send: " + sendVal4 + unitS4); sb.AppendLine("Interface: " + interface6 + " Type: " + IPV6 + " Recv: " + recvVal6 + unitR6 + " Send: " + sendVal6 + unitS6); sb.AppendLine("Interface: " + "BOTH" + " Type: " + "BOTH" + " Recv: " + recvValB + unitRB + " Send: " + sendValB + unitSB); Console.Write(sb.ToString()); } } }