I got two properties from IPv4InterfaceStatistics class, which are BytesReceived and BytesSent for getting the number of bytes that were received and sent from an interface respectively.
I'm getting all of the available network interfaces using NetworkInterface API.
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
But ipv4Stats.BytesSent
and _ipv4Stats.BytesReceived
always returns zero value.
public override void ViewDidLoad()
{
base.ViewDidLoad();
for (int i = 0; i < 10; i++)
{
CheckUsageNetwork();
Thread.Sleep(1000);
}
}
private void CheckUsageNetwork()
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
IPv4InterfaceStatistics _ipv4Stats;
foreach (var nInterface in interfaces)
{
try
{
_ipv4Stats = nInterface.GetIPv4Statistics();
// Hidden speed calculation for brevity
var uploadSpeed = _ipv4Stats.BytesSent;
var downloadSpeed = _ipv4Stats.BytesReceived;
if (uploadSpeed > 0 || downloadSpeed > 0)
{
Console.WriteLine(uploadSpeed + " " + downloadSpeed);
}
}
catch(Exception ex)
{
Console.WriteLine("Exception: {0}", ex);
}
}
}
However, I have used the same code for my win project (WPF), it works.
Can you suggest to me how could I get network usage upload/download speeds on my Xamarin.Mac app? Any kind of help would be appreciated.