I am using following code to detect the name of the Network Interface Card on my PC. I am using a USB Modem.
public class DetectNIC
{
public string ReturnNIC()
{
List<NetworkInterface> Interfaces = new List<NetworkInterface>();
foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
Interfaces.Add(nic);
}
}
NetworkInterface result = null;
foreach (NetworkInterface nic in Interfaces)
{
if (result == null)
{
result = nic;
}
else
{
if (nic.GetIPProperties().GetIPv4Properties() != null)
{
if (nic.GetIPProperties().GetIPv4Properties().Index < result.GetIPProperties().GetIPv4Properties().Index)
result = nic;
}
}
}
return result.Name.ToString();
}
}
I am passing the result to the below given class to calculate the bytes consumption during internet traffic:
public class ByteCounter
{
public double GetNetworkUtilization(string networkCard)
{
const int numberOfIterations = 10;
PerformanceCounter bandwidthCounter =
new PerformanceCounter("Network Interface", "Current Bandwidth", networkCard);
float bandwidth = bandwidthCounter.NextValue();
PerformanceCounter dataSentCounter =
new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkCard);
PerformanceCounter dataReceivedCounter =
new PerformanceCounter("Network Interface", "Bytes Received/sec", networkCard);
float sendSum = 0;
float receiveSum = 0;
for (int index = 0; index < numberOfIterations; index++)
{
sendSum += dataSentCounter.NextValue();
receiveSum += dataReceivedCounter.NextValue();
}
float dataSent = sendSum;
float dataReceived = receiveSum;
double utilization = (8 * (dataSent + dataReceived)) / (bandwidth * numberOfIterations) * 100;
return utilization;
}
}
The first code works fine. It detects my modem as "MBlaze USB Modem", but when the control reaches to the second code with following line:
float bandwidth = bandwidthCounter.NextValue();
it displays error:
System.InvalidOperationException was unhandled
Message="Instance 'MBlaze USB Modem' does not exist in the specified Category."
Source="System"
StackTrace:
at System.Diagnostics.CounterDefinitionSample.GetInstanceValue(String instanceName)
at System.Diagnostics.PerformanceCounter.NextSample()
at System.Diagnostics.PerformanceCounter.NextValue()
at LogTraffic.ByteCounter.GetNetworkUtilization(String networkCard) in F:\Projects\LogTraffic\bytecounter.cs:line 15
at LogTraffic.MainForm.MainForm_Load(Object sender, EventArgs e) in F:\Projects\LogTraffic\MainForm.cs:line 24
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at LogTraffic.Program.Main() in F:\Projects\LogTraffic\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
What is the problem?
I won't forget to give the credit to the authors of both the code.
First code credit: Keyvan Nayyeri