try { System.String sQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"; System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(sQuery); foreach (System.Management.ManagementObject nic in searcher.Get()) { MessageBox.Show(nic["Description"].ToString() + "\n" + ((System.String[]) nic["IPAddress"])[0] + "\n" + ((System.String[]) nic["IPSubnet"])[0] + "\n" + nic["Caption"].ToString() + "\n" + nic["ServiceName"].ToString()); } } catch(Exception Ex) { MessageBox.Show(Ex.ToString()); }This gives me information about the network connections on my computer. Now I got this crazy request to display the name of the network connection, from the name column in the Network Connections in the Control Panel. This name column can be renamed and my users would like to see the name that they gave to the network connection instead of the device name.
//System.String sQuery = "SELECT * FROM Win32_NetworkAdapter WHERE Availability = 3 AND MACAddress <> 'null' AND AdapterTypeID = 0"; System.String sQuery = "SELECT * FROM Win32_NetworkAdapter"; System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(sQuery); foreach (System.Management.ManagementObject nic in searcher.Get()) { if (nic["NetConnectionID"] == null) { //MessageBox.Show(nic["Description"].ToString() + "\n" + "Availability = " + Convert.ToInt16(nic["Availability"]).ToString() + "\n" + "AdapterTypeID = " + Convert.ToInt16(nic["AdapterTypeID"]).ToString(), "Description"); } else { if (nic["MACAddress"] == null) { MessageBox.Show(nic["NetConnectionID"].ToString() + "\n" + "Availability = " + Convert.ToInt16(nic["Availability"]).ToString() + "\n" + "AdapterTypeID = " + Convert.ToInt16(nic["AdapterTypeID"]).ToString() + "\nMAC is null", "NetConnectionID"); } else { MessageBox.Show(nic["NetConnectionID"].ToString() + "\n" + "Availability = " + Convert.ToInt16(nic["Availability"]).ToString() + "\n" + "AdapterTypeID = " + Convert.ToInt16(nic["AdapterTypeID"]).ToString()+"\n"+nic["MACAddress"].ToString(), "NetConnectionID"); } } }NetConnectionID gives me the name as it appears in the name column in the Network Connections Control Panel program, but when NetConnectionID is not null the Availability is equal to 8 which means off line.