- [C#] Ẩn ứng dụng winform từ Apps vào Background Process trên Task Manager
- [SQLSERVER] Xử lý ngoại lệ sử dụng TRY...CATCH và RAISEERROR
- [C#] Bắt sự kiện bàn phím chuột bên ngoài ứng dụng winform sử dụng thư viện MouseKeyHook
- [DEVEXPRESS] Đặt mật khẩu và bỏ mật khẩu tập tin file PDF
- [C#] Thêm ứng dụng vào Taskbar sử dụng SharpShell DeskBand
- [C#] Hướng dẫn thêm text vào hình ảnh icon winform
- [C#] Chia sẽ tổng hợp source code đồ án về Csharp
- [C#] Hướng dẫn viết ứng dụng quay màn hình video winform, Screen Recorder
- [C#] Hướng dẫn sử dụng thư viện Input Simulator để làm việc với Keyboard, Mouse Virtual
- [DEVEXPRESS] Hướng dẫn tích Filter Contain khi click chuột phải vào cell selection trên Gridview
- [C#] Tra cứu mã số thuế cá nhân bằng CMND hoặc CCCD
- [C#] Convert hình ảnh image thành Blurhash sử dụng trong loading image winform
- [POWERSHELL] Script sao lưu backup và nén database sqlserver
- [C#] Giới thiệu thư viện Autofac Dependency Injection
- [C#] Hướng dẫn tạo Windows Services đơn giản Winform
- [C#] Một click chuột điều khiển máy tính từ xa sử dụng Ultraviewer
- Hướng dẫn đóng gói phần mềm sử dụng Powershell biên dịch script thành file exe
- [C#] Hướng dẫn sử dụng Task Dialog trên NET 5
- [C#] Hướng dẫn xem lịch sử các trang web đã truy cập trên Chrome Browser
- [C#] Hướng dẫn lấy thông tin Your ID và Password của Ultraviewer Winform
[C#] Đo băng thông, bandwidth, download, ip và upload traffic
Bài viết hôm nay, mình xin hướng dẫn các bạn code C#, cách xem băng thông và tốc độ download và upload của card internet bằng ngôn ngữ C#.
Băng thông (BandWidth) là gì?
Băng thông tên quốc tế là bandwidth. Thuật ngữ này dùng để chỉ lưu lượng của tín hiệu điện được truyền qua thiết bị truyền dẫn trong một giây là bao nhiêu.
Trong lĩnh vực lưu trữ website, thuật ngữ "băng thông" thường được sử dụng để mô tả số lượng dữ liệu tối đa, mà bạn được phép trao đổi (bao gồm upload và download) qua lại giữa website (hoặc server) và người sử dụng trong một đơn vị thời gian (thường là tháng). Tóm lại, băng thông là thông số chỉ dung lượng tối đa mà website của bạn được lưu chuyển qua lại mỗi tháng.
Giao diện demo ứng dụng:
Souce code bandwidth, upload và download bằng C#
using System;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using System.Collections.Generic;
namespace InterfaceTrafficWatch {
public partial class MainForm: Form {
private
const double timerUpdate = 1000;
private NetworkInterface[] nicArr;
private Timer timer;
public MainForm() {
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e) {
InitializeTimer();
}
private void InitializeNetworkInterface() {
nicArr = NetworkInterface.GetAllNetworkInterfaces();
List goodAdapters = new List();
foreach(NetworkInterface nicnac in nicArr) {
if (nicnac.SupportsMulticast && nicnac.GetIPv4Statistics().UnicastPacketsReceived >= 1 && nicnac.OperationalStatus.ToString() == "Up") {
goodAdapters.Add(nicnac.Name);
cmbInterface.Items.Add(nicnac.Name);
}
}
if (goodAdapters.Count != cmbInterface.Items.Count && goodAdapters.Count != 0) {
cmbInterface.Items.Clear();
foreach(string gadpt in goodAdapters) {
cmbInterface.Items.Add(gadpt);
}
cmbInterface.SelectedIndex = 0;
}
if (goodAdapters.Count == 0) cmbInterface.Items.Clear();
}
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() {
MessageBox.Show(cmbInterface.Items.Count.ToString());
if (cmbInterface.Items.Count >= 1) {
interface NetworkInterface nic = nicArr[cmbInterface.SelectedIndex];
IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();
long bytesSentSpeed = (long)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024;
long bytesReceivedSpeed = (long)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024;
lblSpeed.Text = nic.Speed.ToString();
lblInterfaceType.Text = nic.NetworkInterfaceType.ToString();
lblSpeed.Text = (nic.Speed).ToString("N0");
lblBytesReceived.Text = interfaceStats.BytesReceived.ToString("N0");
lblBytesSent.Text = interfaceStats.BytesSent.ToString("N0");
lblUpload.Text = bytesSentSpeed.ToString() + " KB/s";
lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s";
UnicastIPAddressInformationCollection ipInfo = nic.GetIPProperties().UnicastAddresses;
foreach(UnicastIPAddressInformation item in ipInfo) {
if (item.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
labelIPAddress.Text = item.Address.ToString();
uniCastIPInfo = item;
break;
}
}
}
}
void timer_Tick(object sender, EventArgs e) {
InitializeNetworkInterface();
UpdateNetworkInterface();
}
}
}
Các bạn có thể download code ở bên dưới để tham khảo.
Have fun :)