Can I write a program to limit the use of the Internet to 40 MB per day?

My friend asked me to write a program to limit the use of the Internet to 40 MB per day. If you reach 40 MB of the daily quota, no other programs in the system should have access to the Internet.

+3
source share
4 answers
  • You need to monitor network activity. You can use IPGlobalProperties for this .

    Keep in mind that statistics are reset every time the connection is lost, so you need to store them somewhere.

  • , . /

+10

, , , , , .

EDIT: -, .

, , , . Net Limiter

+4

.

, .

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);
            }
        }
    }
}
+3

cFosSpeed, , / , .

To record it yourself, you need to somehow take into account the amount of data sent so far, and if several computers are on the same network, it will be even more difficult to track.

+1
source

Source: https://habr.com/ru/post/1773236/


All Articles