Created
December 22, 2022 13:26
-
-
Save ljchuello/ee24f9a893cdaf92f176dc86f022ba11 to your computer and use it in GitHub Desktop.
Get networks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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