2

I need to change Network settings like described in this article. That works good so far. However I also need to know on what active network I make the changes.

(For a better understanding please open Control Panel\Network and Internet\ Network and Sharing Center. Unfortunately all picture hosting sites are blocked by my company so I can't post a screenshot.)

Any help on how I can query what connection is associated with what network with WMI (or other technology)?

UPDATE:
I need to query a remote machine.

Community
  • 1
  • 1
gsharp
  • 27,557
  • 22
  • 88
  • 134
  • Here http://networknerd.wordpress.com/2008/09/05/detect-physical-network-adapters-using-wmi/ and here: http://msdn.microsoft.com/en-us/library/aa394216%28v=vs.85%29.aspx should help you – Iridio Sep 10 '11 at 15:26

1 Answers1

5

You can use the NetworkListManager COM component, either with dynamic as shown below or using the Windows API Code Pack which contains all the COM wrappers.

dynamic networkListManager = Activator.CreateInstance(
     Type.GetTypeFromCLSID(new Guid("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")));

var connections = networkListManager.GetNetworkConnections();
foreach (var connection in connections)
{
    var network = connection.GetNetwork();
    Console.WriteLine("Network Name: " + network.GetName());
    Console.WriteLine("Network Category " + 
        network.GetCategory()+ " (0 public / 1 private / 2 Authenticated AD)" );

}

PowerShell:

$networkType = [Type]::GetTypeFromCLSID('DCB00C01-570F-4A9B-8D69-199FDBA5723B')
$networkListManager = [Activator]::CreateInstance($networkType)

$netWorks = $networkListManager.GetNetworkConnections()

foreach ($network in $netWorks)
{
    $name = $network.GetName()
    $category = $network.GetCategory()

    write-host "Network Name: $name"
    write-host "Network Category: $category"
}
TheCodeKing
  • 19,064
  • 3
  • 47
  • 70
  • Thanks, works fine so far. Just I forgot to mention that i need to query a remote machine. – gsharp Sep 12 '11 at 14:03
  • That's make a big difference, you can do it with powershell and remoting. – TheCodeKing Sep 12 '11 at 19:14
  • This works well. Here is a documentation for the methods that can be used: http://msdn.microsoft.com/en-us/library/windows/desktop/aa370750(v=vs.85).aspx – max Dec 29 '14 at 08:22