如何获取网络设备的在线时长?

发布于 2024-10-03 01:17:26 字数 136 浏览 2 评论 0原文

显示网络设备在线时长的图像

我希望我的c#程序能够显示网络设备的在线时长。我已经尝试过 NetworkInterface 类,但它没有该信息。

image showing the online duration of network device

i want my c# program to be able to display the online duration of the network device. i have tried NetworkInterface class but it does not have that info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

递刀给你 2024-10-10 01:17:26

尝试使用 DotRas (http://dotras.codeplex.com/),如网站上所述,“为 C#、VB.NET 和 C++ CLR 项目等 .NET 语言提供远程访问服务 (RAS) 组件”。

通过检查函数 RasGetConnectionStatistics (MSDN 文档)我发现它返回其中包含字段“dwConnectDuration”的结构 (RAS_STATS)。

希望 DotRas 将为您提供一种在 C# 中访问该函数及其返回的所有数据的简单方法。

参考文献:

http://bytes.com/topic/net /answers/484607-字节发送接收网络适配器
http://channel9.msdn.com/ forums/TechOff/69065-使用-C 创建-RAS-连接/

Try getting it through RAS (Remote Access Service), by using DotRas (http://dotras.codeplex.com/), that "Provides remote access service (RAS) components for .NET languages like C#, VB.NET, and C++ CLR projects" as stated on the website.

By checking the function RasGetConnectionStatistics (MSDN documentation) I've found it returns a structure (RAS_STATS) that has the field "dwConnectDuration" in it.

Hopefully DotRas will provide you a easy way to access that function in C#, along with all the data it returns.

References:

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/

゛清羽墨安 2024-10-10 01:17:26

首先,你必须找到这个网络接口设备。您可以使用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);
            }
        }
    }

您还可以检查 这个例子。

First of all, you must find this network interface device. You can do this by using the GetAllNetworkInterfaces().Now, you have a network interface. After, the network interface send this 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);
            }
        }
    }

You can also examine this example.

离去的眼神 2024-10-10 01:17:26

首先添加来自 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() + " NetworkDuration : {0} day(s) {1}:{2}:{3}", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
             }
 }

First Add reference from Network List Manager 1.0 Type Library. Here you can change date as per timezone

    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() + " NetworkDuration : {0} day(s) {1}:{2}:{3}", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
             }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文