0

I want to judge if the computer running my program is in a certain network. I have tried the code below

            ManagementClass vNetworkAdapter = new ManagementClass("Win32_NetworkAdapter");
            ManagementObjectCollection vNetworkAdapters = vNetworkAdapter.GetInstances();
            foreach (ManagementObject vNetworkAdapterInfo in vNetworkAdapters)
            {
                string ID = (string)vNetworkAdapterInfo.Properties["NetConnectionID"].Value;
                string Caption = (string)vNetworkAdapterInfo.Properties["Caption"].Value;
                string Description = (string)vNetworkAdapterInfo.Properties["Description"].Value;
                string SSID = (string)vNetworkAdapterInfo.Properties["DeviceID"].Value;

                if (ID != null)
                {
                    if (ID == "以太网")//judge certain type of connection by name, I only considered the wired and wireless connection
                    {
                        Console.WriteLine("以太网" + "\n" + Caption + "\n" + Description);
                        Console.WriteLine(SSID);
                    }
                    else if (ID == "WLAN")
                    {
                        Console.WriteLine("WLAN" + "\n" + Caption + "\n" + Description);
                        Console.WriteLine(SSID);
                    }
                }
                Console.WriteLine("");
            }

It returns With

WLAN
[00000002] Killer(R) Wi-Fi 6 AX1650i 160MHz Wireless Network Adapter (201NGW)
Killer(R) Wi-Fi 6 AX1650i 160MHz Wireless Network Adapter (201NGW)

以太网
[00000003] Killer E2500 Gigabit Ethernet Controller
Killer E2500 Gigabit Ethernet Controller

But I not wanting this name, I want to get the name shown when we are connecting to a certain network(known as SSID for wireless connection), What I should do? thanks a lot!

  • 1
    Does this answer your question? [Get SSID of the wireless network I am connected to with C# .Net on Windows Vista](https://stackoverflow.com/questions/431755/get-ssid-of-the-wireless-network-i-am-connected-to-with-c-sharp-net-on-windows) – Martinaut Feb 02 '22 at 14:03
  • I found the code I need `using Microsoft.WindowsAPICodePack.Net; var networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected); foreach (var network in networks) { string sConnected = ((network.IsConnected == true) ? " (connected)" : " (disconnected)"); Console.WriteLine("Network : " + network.Name + " - Category : " + network.Category.ToString() + sConnected); }` – IShirai_KurokoI Feb 03 '22 at 06:57

0 Answers0