WMI for quering active networks and associate conn

2019-07-11 21:06发布

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.

标签: c# .net c#-4.0 wmi
1条回答
Emotional °昔
2楼-- · 2019-07-11 21:53

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"
}
查看更多
登录 后发表回答