Can i write a program to limit the Internet usage to 40mb per day?
M开发者_开发百科y Friend asked me to write a program to limit the Internet usage to 40 Mb per day. If the 40 Mb of daily quota is reached no other programs in the system must be able to access the Internet.
You need to monitor the network activity. One can use IPGlobalProperties class for that.
Keep in mind that the statistics are reseted each time the connection is lost so you'll have to store them somewhere.
You need to disable internet connection, see Code to enable/disable internet connectivity
Tell him you wrote a program, but instead hire a guy to watch his internet usage, and pull out the plug when the limit is hit.
EDIT: Apparently my sense of humor is off.
Anyway I think this would be quite an undertaking, I could not find any code for it. But I did find this Net Limiter
Yes.
More of a proof of concept than a working solution.
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);
}
}
}
}
This should be doable in a router or with cFosSpeed which also provides quotas, however I do now know any freeware/open-source applications that already do this.
For writing it yourself you'd have to somehow keep count of the amount of data sent so far, and if multiple computers are on the same network it's going to be even harder to keep track of.
精彩评论