bandwidthCounter.CategoryName = "Network Interface"; bandwidthCounter.CounterName = "Current Bandwidth"; bandwidthCounter.InstanceName = networkCard; bandwidth = bandwidthCounter.NextValue(); // Network card name totalDataCounter.CategoryName = "Network Interface"; totalDataCounter.CounterName = "Bytes Total/sec"; totalDataCounter.InstanceName = networkCard; // Network card name receiveSum = totalDataCounter.NextValue(); Console.WriteLine("Bandwidth is " + bandwidth.ToString()); Console.WriteLine("Total bytes is " + receiveSum.ToString()); double utilization = ((8 * receiveSum) / (bandwidth)) * 100;
namespace NetworkUtilService { public partial class NetworkUtil : ServiceBase { System.Timers.Timer aTimer; public NetworkUtil() { InitializeComponent(); } protected override void OnStart(string[] args) { // TODO: Add code here to start your service. aTimer = new System.Timers.Timer(); aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent); aTimer.Interval = 1000; aTimer.Enabled = true; } protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. } public void GetNetWorkUtil() { float sendSum = 0; float receiveSum = 0; float bandwidth = 0; string networkCard = "Broadcom NetXtreme 57xx Gigabit Controller"; PerformanceCounter bandwidthCounter = new PerformanceCounter(); bandwidthCounter.CategoryName = "Network Interface"; bandwidthCounter.CounterName = "Current Bandwidth"; bandwidthCounter.InstanceName = networkCard; bandwidth = bandwidthCounter.NextValue(); PerformanceCounter totalDataCounter = new PerformanceCounter(); totalDataCounter.CategoryName = "Network Interface"; totalDataCounter.CounterName = "Bytes Total/sec"; totalDataCounter.InstanceName = networkCard; receiveSum = totalDataCounter.NextValue(); Debug.OutputDebugString("Total bytes: " + receiveSum.ToString() + " at Time " + DateTime.Now.Second.ToString()); double utilization = ((8 * receiveSum) / (bandwidth)) * 100; } private void OnTimedEvent(object source, ElapsedEventArgs e) { GetNetWorkUtil(); } }
namespace NetworkUtilization { class Calculate { PerformanceCounter bandwidthCounter = null; PerformanceCounter totalDataCounter = null; string networkCard = null; public Calculate() { bandwidthCounter = new PerformanceCounter(); totalDataCounter = new PerformanceCounter(); networkCard = "Broadcom NetXtreme 57xx Gigabit Controller"; SetTimer(); Console.ReadLine(); } public void SetTimer() { Timer myTimer = new System.Timers.Timer(); myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed); myTimer.Enabled = true; myTimer.Interval = 1000; } public void myTimer_Elapsed(object sender, ElapsedEventArgs e) { float receiveSum = 0; float bandwidth = 0; bandwidthCounter.CategoryName = "Network Interface"; bandwidthCounter.CounterName = "Current Bandwidth"; bandwidthCounter.InstanceName = networkCard; bandwidth = bandwidthCounter.NextValue(); totalDataCounter.CategoryName = "Network Interface"; totalDataCounter.CounterName = "Bytes Total/sec"; totalDataCounter.InstanceName = networkCard; receiveSum = totalDataCounter.NextValue(); Console.WriteLine("Bandwidth is " + bandwidth.ToString()); Console.WriteLine("Total bytes is " + receiveSum.ToString()); double utilization = ((8 * receiveSum) / (bandwidth)) * 100; Console.WriteLine("Utilization is " + utilization.ToString() + " " + DateTime.Now.ToString()); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)