简体   繁体   English

获取网络接口所连接的网络的名称

[英]Get name of network that a network interface is connected to

In Windows Control Panel, you can find a list of network interfaces/connections which displays the following: 在Windows控制面板中,您可以找到显示以下内容的网络接口/连接列表:

网络接口

In the .NET framework these are represented in the NetworkInterface class (and found via NetworkInterface.GetAllNetworkInterfaces ). 在.NET框架中,它们在NetworkInterface类中表示(并通过NetworkInterface.GetAllNetworkInterfaces找到)。

For reference, say I'm reading properties from the Ethernet interface - the NetworkInterface.Name property returns "Ethernet" , the NetworkInterface.Description property returns "Realtek PCIe FE Family Controller" . 作为参考,说我正在从以太网接口读取属性NetworkInterface.Name属性返回"Ethernet"NetworkInterface.Description属性返回"Realtek PCIe FE Family Controller"

However, nothing in the class seems to be able to get me the name of the network it's connected to (in this case "BELL024" ). 但是,该类中似乎没有任何内容可以让我知道它所连接的网络的名称(在本例中为"BELL024" )。 How would I go about getting that string? 我将如何获取该字符串? I have to know what network the interface is associated with, not just a list of the networks that exist. 我必须知道接口与哪个网络相关联,而不仅是现有网络的列表。

It turns out information about each network is stored as a 'network profile' by Windows, storing it's name and other info like whether it's public or not. 事实证明,有关每个网络的信息都由Windows作为“网络配置文件”存储,并存储其名称和其他信息(如是否公开)。 The name can be changed by users in the control panel, but in my situation that's not a problem. 用户可以在控制面板中更改名称,但就我而言,这不是问题。

The Windows API Code Pack from Microsoft contains the APIs necessary to get the collection of network profiles. Microsoft的Windows API代码包包含获取网络配置文件所需的API。 As it contains a lot of bloat that I don't need, the bare minimum code to wrap the Windows API can be found here . 由于它包含了很多我不需要的膨胀, 因此可以在此处找到包装Windows API的最基本代码。

A collection of the network profiles can then be found like so: 然后可以找到网络配置文件的集合,如下所示:

//Get the networks that are currently connected to
var networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected);

Each object in the collection represents a network profile and contains a collection of NetworkConnection objects. 集合中的每个对象代表一个网络配置文件,并包含一个NetworkConnection对象的集合。 Each NetworkConnection object appears to be info about an interface's connection to the base network. 每个NetworkConnection对象似乎都是有关接口与基本网络的连接的信息。

foreach(Network network in networks)
{
    //Name property corresponds to the name I originally asked about
    Console.WriteLine("[" + network.Name + "]");

    Console.WriteLine("\t[NetworkConnections]");
    foreach(NetworkConnection conn in network.Connections)
    {
        //Print network interface's GUID
        Console.WriteLine("\t\t" + conn.AdapterId.ToString());
    }
}

The NetworkConnection.AdapterId property is the same network interface GUID that the NetworkInterface.Id property knows. NetworkConnection.AdapterId属性是NetworkInterface.Id属性知道的相同网络接口GUID。

So, you can determine what network an interface is connected to, by checking if one of the network's connections have the same ID as the interface. 因此,可以通过检查网络连接之一是否具有与该接口相同的ID来确定接口连接到哪个网络。 Note that they're represented differently, so you'll have to do a bit more work: 请注意,它们的表示方式有所不同,因此您需要做更多的工作:

示例图片

Both my Wi-Fi and Ethernet interfaces are connected to the BELL024 network in the above example. 在上面的示例中,我的Wi-Fi和以太网接口都连接到BELL024网络。

You can use WMI to query for your network name. 您可以使用WMI查询您的网络名称。 You can use this code as a sample: 您可以使用以下代码作为示例:

ManagementScope oMs = new ManagementScope();  
ObjectQuery oQuery =  
    new ObjectQuery("Select * From Win32_NetworkAdapter");  
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);  
ManagementObjectCollection oReturnCollection = oSearcher.Get();  
foreach (ManagementObject oReturn in oReturnCollection) 
{
    if (oReturn.Properties["NetConnectionID"].Value != null)  
        {
            Console.WriteLine("Name : " + oReturn.Properties["NetConnectionID"].Value);
        }
} 

On Windows 8 and Windows 2012 and higher you can query WMI class MSFT_NetConnectionProfile from root/StandardCimv2 namespace. 在Windows 8和Windows 2012及更高版本上,您可以从root / StandardCimv2命名空间查询WMI类MSFT_NetConnectionProfile

It shouldn't be too hard to convert this to C#. 将其转换为C#并不难。

Get-WmiObject -Namespace root/StandardCimv2 -Class MSFT_NetConnectionProfile | Format-Table InterfaceAlias, Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM