private static void Update(object state)
    {
        ulong  speed   = 0;
        string adapter = "";

        string[] nameSearches = { "Wireless", "WiFi", "802.11", "Wi-Fi" };
        // The enum value of `AF_INET` will select only IPv4 adapters.
        // You can change this to `AF_INET6` for IPv6 likewise
        // And `AF_UNSPEC` for either one
        foreach (IPIntertop.IP_ADAPTER_ADDRESSES net in IPIntertop.GetIPAdapters(IPIntertop.FAMILY.AF_INET))
        {
            bool containsName = false;
            foreach (string name in nameSearches)
            {
                if (net.FriendlyName.Contains(name))
                {
                    containsName = true;
                }
            }
            if (!containsName)
            {
                continue;
            }
            speed   = net.TrasmitLinkSpeed;
            adapter = net.FriendlyName;
            break;
        }

        string temp;

        if (speed == 0)
        {
            temp = "There is currently no Wi-Fi connection";
        }
        else
        {
            temp = string.Format("Current Wi-Fi Speed: {0} Mbps on {1}", (speed / 1000000.0), adapter);
        }

        Console.WriteLine(temp);
    }
Ejemplo n.º 2
0
    private static void DisplayWiFiAddresses()
    {
        string[] nameSearches = { "Wireless", "WiFi", "802.11", "Wi-Fi" };

        // AF_INET for IPv4, AF_INET6 for IPv6, and `AF_UNSPEC` for both
        foreach (IPIntertop.IP_ADAPTER_ADDRESSES net in IPIntertop.GetIPAdapters(IPIntertop.FAMILY.AF_UNSPEC))
        {
            bool containsName = false;
            foreach (string name in nameSearches)
            {
                if (net.FriendlyName.Contains(name))
                {
                    containsName = true;
                }
            }
            if (!containsName)
            {
                continue;
            }

            Alignment alignment = new Alignment(net.Alignment);

            Console.WriteLine("\tLength of the IP_ADAPTER_ADDRESS struct: " + alignment.Length);
            Console.WriteLine("\tIfIndex (IPv4 interface): " + alignment.IfIndex);
            Console.WriteLine("\tAdapter name: " + Marshal.PtrToStringAnsi(net.AdapterName));
            Console.WriteLine("\tDNS Suffix: " + net.DnsSuffix);
            Console.WriteLine("\tDescription: " + net.Description);
            Console.WriteLine("\tFriendly name: " + net.FriendlyName);

            if (net.PhysicalAddressLength != 0)
            {
                Console.WriteLine("\tPhysical address: " + BitConverter.ToString(net.PhysicalAddress.Take((int)net.PhysicalAddressLength).ToArray()));
            }
            Console.WriteLine("\tFlags: " + net.Flags);
            Console.WriteLine("\tMtu: " + net.Mtu);
            Console.WriteLine("\tIfType: " + net.IfType);
            Console.WriteLine("\tOperStatus: " + net.OperStatus);

            Console.WriteLine("\tIpv6IfIndex (IPv6 interface): " + net.Ipv6IfIndex);

            Console.Write("\tZoneIndices (hex): ");
            for (int i = 0; i < net.ZoneIndices.Length; i++)
            {
                Console.Write(net.ZoneIndices[i].ToString("X") + " ");
            }
            Console.WriteLine("");

            Console.WriteLine("\tTransmit link speed: " + net.TransmitLinkSpeed);
            Console.WriteLine("\tReceive link speed: " + net.ReceiveLinkSpeed);

            IntPtr prefix  = net.FirstPrefix;
            int    counter = 0;
            for (int i = 0; prefix != IntPtr.Zero; i++)
            {
                IPIntertop.IP_ADAPTER_PREFIX tmp;
                tmp    = (IPIntertop.IP_ADAPTER_PREFIX)Marshal.PtrToStructure(prefix, typeof(IPIntertop.IP_ADAPTER_PREFIX));
                prefix = tmp.Next;
                counter++;
            }
            Console.WriteLine("\tNumber of IP Adapter Prefix entries: " + counter);

            Console.WriteLine("");
        }
    }