代码之家  ›  专栏  ›  技术社区  ›  Vishnu Pradeep

我可以写一个程序来限制每天40兆的互联网使用量吗?

  •  3
  • Vishnu Pradeep  · 技术社区  · 14 年前

    我的朋友让我写一个程序,把每天的互联网使用量限制在40MB。如果达到每日40MB的配额,则系统中没有其他程序必须能够访问Internet。

    4 回复  |  直到 14 年前
        2
  •  4
  •   Iain    14 年前
        3
  •  3
  •   Greg Buehler    14 年前

    using System;
    using System.Linq;
    using System.Threading;
    using System.Net.NetworkInformation;
    
    namespace ConsoleApplication1
    {
        class Program
        {
    
    
            static void Main(string[] args)
            {
                const double ShutdownValue = 40960000D;
                const string NetEnable = "interface set interface \u0022{0}\u0022 DISABLED";
                const string NetDisable = "interface set interface \u0022{0}\u0022 ENABLED";
    
                double Incoming = 0;
                double Outgoing = 0;
                double TotalInterface;
                string SelectedInterface = "Local Area Connection";
    
                NetworkInterface netInt = NetworkInterface.GetAllNetworkInterfaces().Single(n => n.Name.Equals(SelectedInterface));
    
                for (; ; )
                {
                    IPv4InterfaceStatistics ip4Stat = netInt.GetIPv4Statistics();
    
                    Incoming += (ip4Stat.BytesReceived - Incoming);
                    Outgoing += (ip4Stat.BytesSent - Outgoing);
                    TotalInterface = Incoming + Outgoing;
    
                    string Shutdown = ((TotalInterface > ShutdownValue) ? "YES" : "NO");
    
                    if (Shutdown == "YES")
                    {
                        System.Diagnostics.Process.Start("netsh", string.Format(NetDisable, SelectedInterface));
                    }
    
                    string output = string.Format("Shutdown: {0} | {1} KB/s", Shutdown, TotalInterface.ToString());
                    Console.WriteLine(output);
    
                    Thread.Sleep(3000);
                }
            }
        }
    }
    
        4
  •  1
  •   Not Available    14 年前