答案 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);
}
}