用于查询活动网络和关联连接的WMI

时间:2011-09-01 09:47:55

标签: c# .net c#-4.0 wmi

我需要更改this article中所述的网络设置。到目前为止这很好用。但是,我还需要了解我所做的更改的活动网络。

(为了更好地理解,请打开Control Panel\Network and Internet\ Network and Sharing Center。不幸的是,所有图片托管网站都被我公司阻止,因此我无法发布截图。)

有关如何查询哪些连接与WMI(或其他技术)的网络相关联的任何帮助?

更新:
我需要查询远程机器。

1 个答案:

答案 0 :(得分:5)

您可以使用NetworkListManager COM组件,如下所示使用动态,或使用包含所有COM包装器的Windows API Code Pack

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"
}