2010-11-17 33 views
1

image showing the online duration of network device如何獲取網絡設備的在線時長?

我希望我的c#程序能夠顯示網絡設備的在線持續時間。我試過NetworkInterface類,但它沒有這個信息。

+0

WMI很可能有性能計數器。我尋找5分鐘,我找不到一個,但它可能在一個不太明顯的地方。 – Aren 2010-11-17 01:17:07

+0

DHCP租約獲取時間是否接近網絡持續時間? – vishnu 2010-11-17 11:20:41

回答

1

嘗試通過RAS(遠程訪問服務),通過使用DotRas(http://dotras.codeplex.com/),「爲.NET語言(如C#,VB.NET和C++ CLR項目)提供遠程訪問服務(RAS)組件」在網站上。

通過檢查函數RasGetConnectionStatistics(MSDN documentation),我發現它返回一個結構(RAS_STATS),其中包含字段「dwConnectDuration」。

希望DotRas能爲您提供一種簡單的方式來訪問C#中的函數以及它返回的所有數據。

參考文獻:

所有的

http://bytes.com/topic/net/answers/484607-bytes-sent-received-network-adapter http://channel9.msdn.com/forums/TechOff/69065-Creating-a-RAS-connection-with-C/

1

首先,你必須找到這個網絡接口設備。您可以通過使用GetAllNetworkInterfaces()來完成此操作。現在,您有一個網絡接口。之後,網絡接口發送這個methot。

static void getCurrentNicLifeTime(NetworkInterface adapter) 
    { 
     IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); 

     UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses; 

     if (uniCast.Count > 0) 
     { 
      foreach (UnicastIPAddressInformation uni in uniCast) 
      { 
       DateTime when; 
       when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressValidLifetime) - TimeSpan.FromSeconds(864000); 
       when = when.ToLocalTime(); 

       Console.WriteLine(DateTime.UtcNow.ToLocalTime() - when); 
      } 
     } 
    } 

您還可以檢查this示例。

相關問題