How to get the connection name used by the Windows network adapter?

Is it possible to get it using a WMI request?

my current code is:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                                       "SELECT * FROM Win32_NetworkAdapte");

foreach (ManagementObject queryObj in searcher.Get())
{
     Console.WriteLine(queryObj[??]);        
}

I tried to get the connection name from:

Control Panel \ Network and Internet \ Network Connections
+5
source share
1 answer

Using the code below, you can reset all the properties of the network adapter, you need the Nameproperty:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
    "SELECT * FROM Win32_NetworkAdapter");

foreach (ManagementObject adapter in searcher.Get())
{
    StringBuilder propertiesDump = new StringBuilder();
    foreach (var property in adapter.Properties)
    {
        propertiesDump.AppendFormat(
            "{0} == {1}{2}", 
            property.Name, 
            property.Value, 
            Environment.NewLine);        
    }
}

OR just using LINQ (add using System.Linq):

foreach (ManagementObject adapter in searcher.Get())
{
   string adapterName = adapter.Properties
                               .Cast<PropertyData>()
                               .Single(p => p.Name == "Name")
                               .Value.ToString();
}

PS: Also remember that you have a typo in the WMI request - forgot rin Adapter: Win32_NetworkAdapte_r _

+3
source

All Articles