代码之家  ›  专栏  ›  技术社区  ›  Vishnu Pradeep

如何获取网络设备的联机持续时间?

  •  1
  • Vishnu Pradeep  · 技术社区  · 14 年前

    image showing the online duration of network device

    我希望我的c#程序能够显示网络设备的联机持续时间。我试过了 NetworkInterface 类,但它没有该信息。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Telmo Marques    14 年前

    尝试使用DotRas通过RAS(远程访问服务)获取它( http://dotras.codeplex.com/ “如VB.NET所说,”为C语言、C语言和C++ CLR项目提供.NET语言的远程访问服务(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/

        2
  •  1
  •   Emre Abacı    9 年前

    首先,你必须找到这个网络接口设备。您可以使用 GetAllNetworkInterfaces() 。现在,你有了一个网络接口。之后,网络接口发送此方法。

    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 例子。