Skip to content

Instantly share code, notes, and snippets.

@ljchuello
Created December 22, 2022 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljchuello/ee24f9a893cdaf92f176dc86f022ba11 to your computer and use it in GitHub Desktop.
Save ljchuello/ee24f9a893cdaf92f176dc86f022ba11 to your computer and use it in GitHub Desktop.
Get networks
using System.Diagnostics;
using System.Net.NetworkInformation;
namespace Prueba
{
internal class Program
{
static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
try
{
decimal oNewIn = 0, oNewOut = 0, oOldIn = 0, oOldOut = 0;
while (true)
{
if (!NetworkInterface.GetIsNetworkAvailable())
return;
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
oNewIn = oNewIn + (decimal)ni.GetIPv4Statistics().BytesReceived;
oNewOut = oNewOut + (decimal)ni.GetIPStatistics().BytesSent;
}
// KB
oNewIn = oNewIn / 1024;
oNewOut = oNewOut / 1024;
// Validamos
if (oOldIn == 0 || oOldOut == 0)
{
oOldIn = oNewIn;
oOldOut = oNewOut;
}
// Diferencia
Console.WriteLine($"In {(oNewIn - oOldIn):n2} - {(oNewOut - oOldOut):n2}");
oOldIn = oNewIn;
oOldOut = oNewOut;
await Task.Delay(1000);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment