1
A
回答
0
您可以使用System.Diagnostics
命名空間中的PerformanceCounter
類來完成此操作。
記下兩個班,這將幫助您實現您想要做的事情。
NetworkMonitor
類
public class NetworkMonitor
{
private ArrayList adapters;
public ArrayList monitoredAdapters;
public NetworkMonitor()
{
this.adapters = new ArrayList();
this.monitoredAdapters = new ArrayList();
EnumerateNetworkAdapters();
}
private void EnumerateNetworkAdapters()
{
PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
foreach (string name in category.GetInstanceNames())
{
if (name == "MS TCP Loopback interface")
continue;
NetworkAdapter adapter = new NetworkAdapter(name);
adapter.dlCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", name);
adapter.ulCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", name);
this.adapters.Add(adapter);
adapter.init();
}
}
public NetworkAdapter[] Adapters
{
get
{
return (NetworkAdapter[])this.adapters.ToArray(typeof(NetworkAdapter));
}
}
}
NetworkAdapter
類
public class NetworkAdapter
{
// Instances of this class are supposed to be created only in an NetworkMonitor.
internal NetworkAdapter(string name)
{
this.name = name;
}
private long dlSpeed, ulSpeed;
private long dlValue, ulValue;
private long dlValueOld, ulValueOld;
internal string name;
internal PerformanceCounter dlCounter, ulCounter;
internal bool Enabled;
internal void init()
{
this.dlValueOld = this.dlCounter.NextSample().RawValue;
this.ulValueOld = this.ulCounter.NextSample().RawValue;
this.Enabled = true;
}
// Obtain new sample from performance counters, and refresh the values saved in dlSpeed, ulSpeed, etc.
// This method is supposed to be called only in NetworkMonitor, one time every second.
internal void refresh()
{
this.dlValue = this.dlCounter.NextSample().RawValue;
this.ulValue = this.ulCounter.NextSample().RawValue;
// Calculates download and upload speed.
this.dlSpeed = this.dlValue - this.dlValueOld;
this.ulSpeed = this.ulValue - this.ulValueOld;
this.dlValueOld = this.dlValue;
this.ulValueOld = this.ulValue;
}
// Overrides method to return the name of the adapter.
public override string ToString()
{
return this.name;
}
public string Name
{
get
{
return this.name;
}
}
// Current download speed in bytes per second.
public long DownloadSpeed(int Interval)
{
return this.dlSpeed * 1000/Interval;
}
// Current upload speed in bytes per second.
public long UploadSpeed(int Interval)
{
return this.ulSpeed * 1000/Interval;
}
}
所有你需要做的是:
long downloadSpeed = 0;
long uploadSpeed = 0;
NetworkMonitor networkMonitor = new NetworkMonitor();
foreach (NetworkAdapter adapter in monitor.Adapters)
{
if (adapter.Enabled)
{
adapter.refresh();
downloadSpeed += adapter.DownloadSpeed(yourTimerInterval);
uploadSpeed += adapter.UploadSpeed(yourTimerInterval);
}
}
希望這有助於。 此致敬禮。
相關問題
- 1. 計算數據傳輸速率
- 2. NPP:重疊計算和數據傳輸
- 3. 傳輸數據
- 4. 傳輸數據
- 5. 計算下載/上傳速度
- 6. SQL數據傳輸
- 7. SQL傳輸數據
- 8. 傳輸數據DB
- 9. Python數據傳輸
- 10. SHAREPoint數據傳輸
- 11. 計算上傳傳輸速度問題
- 12. 數據傳輸體系結構設計
- 13. XML數據傳輸的設計決策
- 14. 如何計算傳輸文件上傳的字節數?
- 15. iframe傳輸不傳輸任何數據
- 16. 如何從網頁與後臺計算機共享數據並傳輸數據
- 17. 如何計算python數據傳輸每秒ifconfig?
- 18. 遠程計算機之間的數據傳輸
- 19. 從移動應用程序到計算機的數據傳輸
- 20. 局域網內兩臺計算機之間的數據傳輸
- 21. 如何在兩臺計算機之間傳輸數據?
- 22. 帶寬計算(互聯網數據傳輸)
- 23. PHP上傳數據
- 24. 傳輸數據到PostgreSQL數據庫
- 25. 主數據服務傳輸數據
- 26. 在AWS上傳輸SQL數據庫
- 27. 在Windows Azure上監控數據傳輸?
- 28. onActivityResult上的包裹數據傳輸
- 29. 如何上傳和下載iphone服務器上的數據(ios)
- 30. Template.dynamic不傳遞數據上下文
嘿thnks爲您的幫助bt其下載一個複製速度...我希望它..下載的數據量lyk 30 MB或35 MB ..等.. – 2010-06-25 11:27:40
每秒運行一段代碼,並且加起來你想保留的地方。 '有沒有確定的方式來獲得總金額' – 2010-06-25 11:49:36
okkzz.thnkxx很多.. – 2010-06-28 10:11:46