Vishnu Pradeep
Vishnu Pradeep

Reputation: 2097

How to get online duration of the network device?

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.

Upvotes: 1

Views: 1046

Answers (3)

Navneet Bhalodiya
Navneet Bhalodiya

Reputation: 434

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

Upvotes: 1

Emre Abacı
Emre Abacı

Reputation: 11

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.

Upvotes: 1

Telmo Marques
Telmo Marques

Reputation: 5106

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/

Upvotes: 1

Related Questions