Get the Available Network Connections Using WMI

March 30, 2009 | By

Available Networks

Windows Management Instrumentation (WMI) makes querying Windows for system and devices information easy. And this is even easier with System.Management assembly. The code listed loads the names of the network connections in a ListBox, which when clicked displays all the available properties in a ListView.

To get the code to work add a reference to System.Management. The form’s Load event retrieves the list of network connections that use IP and loops through them adding each one’s description property to the ListBox. The ListBox’s SelectedIndexChanged event executes the same query again but adds to the WHERE clause the Description property. The result is the object selected by the user.

 

Download Source Code

 

Filed in: WinForms | Tags: , ,

Comments (6)

Links to this Post

  1. DotNetShoutout | April 5, 2009
  1. it’s nice to stay in your blog..

  2. Ralph Bautista

    will this detect if I am still connected online? or will it only detect the network connections available?

  3. @Ralph: No this only lists the available connections, to check if the computer is connected to a network (whether the Internet or a local network) use:

    NetworkInterface.GetIsNetworkAvailable()

  4. Alok kumar sharma

    Hi Amgad,

    How can access network adapter name of the virtual Machines using C#.All Virtual Machine are intsalll on the Hyper-v.

    Thanks
    Alok kumar sharma

  5. Alok Kumar Sharma

    Hi Amgad,

    Issue regarding list of network adapter in Hyper-V Manager using C#

    1. I have a virtual machine (named HV4), it contains Windows 2012 Server. Hyper-V Manager is enabled on this machine.
    2. There are more than one virtual machines under Hyper-V Manager (HV4 nested) . All machines contains Windows 2012 Server.
    3. All the machine are connected with External switch and assigned the IP 192.168.x.x. series.

    The description of the issue is as below:
    Now I want to get list of names of all network adapters of each Virtual Machine like; NetConnectionID (Local Area Connection1, Local Area Connection 2 etc.. ) using C#,WMI (Windows Management Instrumentation) with using System.Management;

    I am using CIM_NetworkAdapter Class (http://msdn.microsoft.com/en-us/library/aa387931%28v=vs.85%29.aspx).

    1. When the name of the virtual machine in Hyper-V Manager and Computer name of that machine is same then its working fine.
    2. If I change the Computer name of virtual machine then I get an error Message : “The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)”. But it works fine in the above case (i.e. case 1).

    The used code is given below:
    // string strVMName is Name of Virtual Machine .
    private void GetNetAdapterInfo(string strVMName)
    {
    string strVirtualMachineName = string.Empty;
    StringBuilder strEthernet = new StringBuilder();
    try
    {
    //create a management scope object
    // strVMName= Virtual Machine Name like VM2008R2
    ManagementScope scope = new ManagementScope(“\\\\”+strVMName+”\\ROOT\\cimv2”);

    //create object query
    ObjectQuery query = new ObjectQuery(“SELECT * FROM CIM_NetworkAdapter”);

    //create object searcher
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    //get collection of WMI objects
    ManagementObjectCollection queryCollection = searcher.Get();
    //enumerate the collection.
    foreach (ManagementObject m in queryCollection)
    {
    // access properties of the WMI object
    foreach (PropertyData propNet in m.Properties)
    {
    strEthernet.Append(propNet.Name + ” :\t” + propNet.Value);
    strEthernet.Append(“\n”);
    }
    }

    }
    catch (Exception expGetNetAdapterInfo)
    {
    MessageBox.Show(expGetNetAdapterInfo.GetBaseException().ToString());
    }
    MessageBox.Show(strEthernet.ToString());
    }

    Please help me.