我希望我的c#程序能够显示网络设备的在线时长。我已经尝试了NetworkInterface
课程,但它没有这个信息。
答案 0 :(得分:1)
尝试使用DotRas(http://dotras.codeplex.com/)通过RAS(远程访问服务)获取它,“为C#,VB.NET和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/
答案 1 :(得分: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示例。
答案 2 :(得分: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);
}
}