如何使用C#获取当前正在工作的互联网连接的持续时间信息?

时间:2015-08-23 14:30:12

标签: c# windows

当我们查看Windows中当前互联网连接的一般信息时,我们可以轻松找到持续时间。有谁知道如何在Windows中使用C#或C / C ++实用地检索这些信息?

Duration of internet connection

非常感谢

亚伦

2 个答案:

答案 0 :(得分:0)

您只需使用NetworkInformation;

using System.Net.NetworkInformation;

NetworkAvailabilityChanged

添加事件方法方法
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkConnectivityStatusChanged);    

void NetworkConnectivityStatusChanged(object sender, NetworkAvailabilityEventArgs e)
{
        ONLINE = e.IsAvailable;
}

我想我误解了这个问题,但您可以在Changed事件中添加自己的计时器,当它脱机时您可以重置它

答案 1 :(得分:0)

首先从Network List Manager 1.0类型库添加引用

        var manager = new NetworkListManager();
        var connectedNetworks = manager.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED).Cast<INetwork>();
        foreach (var network in connectedNetworks)
        {
            if (network.IsConnected)
            {
              network.GetTimeCreatedAndConnected(out uint _, out uint _, out uint 
              pdwLowDateTimeConnected, out uint pdwHighDateTimeConnected);

              DateTime networkConnectedTime = DateTime.FromFileTimeUtc((long) 
            (((ulong)pdwHighDateTimeConnected << 32) | pdwLowDateTimeConnected));

            TimeSpan diff = DateTime.Now.Subtract(networkConnectedTime);

            Console.WriteLine("Name: " + network.GetName() + "  Duration : {0} day(s) {1}:{2}:{3}", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
                 }
     }