Thứ Tư, 5 tháng 8, 2009

Đo dung lượng internet bằng C#

Author by : Mohamed Mansour,

How to calculate network bandwidth speed in c#

http://www.m0interactive.com/archives/2008/02/06/how_to_calculate_network_bandwidth_speed_in_c_.html


Hôm nọ. đọc trong ddth.com có 1 bạn nhờ tìm về code đo dung lượng tính tóan dung lượng internet bằng C#. Mình hứng chí lên, tìm tầm 5 phút là ra cái đoạn code này. Post lên đây. khi nào sẽ có lúc dùng.

Đầu tiên là hàm kiểm tra máy tính có kết nối với Internet không : (không dùng trong code)
static uint uConnection = 0x20;
[System.Runtime.InteropServices.DllImport("wininet.dll")]

private static extern bool InternetGetConnectedState(ref uint connected, uint reserved);
//-----Còn đây là code chạy OK trên C# nè. He he.
Giao diện :


Code :
using System;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace InterfaceTrafficWatch
{
///
/// Network Interface Traffic Watch
/// by Mohamed Mansour
///
/// Free to use under GPL open source license!
///
public partial class MainForm : Form
{
///
/// Timer Update (every 1 sec)
///
private const double timerUpdate = 1000;

///
/// Interface Storage
///
private NetworkInterface[] nicArr;

///
/// Main Timer Object
/// (we could use something more efficient such
/// as interop calls to HighPerformanceTimers)
///
private Timer timer;

///
/// Constructor
///
public MainForm()
{
InitializeComponent();
InitializeNetworkInterface();
InitializeTimer();
}

///
/// Initialize all network interfaces on this computer
///
private void InitializeNetworkInterface()
{
// Grab all local interfaces to this computer
nicArr = NetworkInterface.GetAllNetworkInterfaces();

// Add each interface name to the combo box
for (int i = 0; i < nicArr.Length; i++)
cmbInterface.Items.Add(nicArr[i].Name);

// Change the initial selection to the first interface
cmbInterface.SelectedIndex = 0;
}

///
/// Initialize the Timer
///
private void InitializeTimer()
{
timer = new Timer();
timer.Interval = (int)timerUpdate;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}

///
/// Update GUI components for the network interfaces
///
private void UpdateNetworkInterface()
{
// Grab NetworkInterface object that describes the current interface
NetworkInterface nic = nicArr[cmbInterface.SelectedIndex];

// Grab the stats for that interface
IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();

// Calculate the speed of bytes going in and out
// NOTE: we could use something faster and more reliable than Windows Forms Tiemr
// such as HighPerformanceTimer http://www.m0interactive.com/archives/2006/12/21/high_resolution_timer_in_net_2_0.html
int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024;
int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024;

// Update the labels
lblSpeed.Text = nic.Speed.ToString();
lblInterfaceType.Text = nic.NetworkInterfaceType.ToString();
lblSpeed.Text = nic.Speed.ToString();
lblBytesReceived.Text = interfaceStats.BytesReceived.ToString();
lblBytesSent.Text = interfaceStats.BytesSent.ToString();
lblUpload.Text = bytesSentSpeed.ToString() + " KB/s";
lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s";

}

///
/// The Timer event for each Tick (second) to update the UI
///
///
///
void timer_Tick(object sender, EventArgs e)
{
UpdateNetworkInterface();
}

}
}

Không có nhận xét nào:

Đăng nhận xét