如何确定一个接口是否是 Windows PC 上的物理 LAN 端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/203709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:27:53  来源:igfitidea点击:

How do you determine if an interface is a physical LAN port on a Windows PC?

windowswmiadminipconfig

提问by monkut

I'm looking for a method to acurately determine if an interface is the physical 802.3 ethernet port on a pc in windows.

我正在寻找一种方法来准确确定接口是否是 Windows 中 PC 上的物理 802.3 以太网端口。

Using ipconfig /all I can list all the interfaces, and when I do this on my pc several entries can be listed here including VPN, Bluetooth, Wifi and the physical ethernet interface.

使用 ipconfig /all 我可以列出所有接口,当我在我的电脑上这样做时,可以在此处列出几个条目,包括 VPN、蓝牙、Wifi 和物理以太网接口。

I'm looking for something like, "isPhysical(interface)". (It's ok to have multiple physical lan ports, I just want to know if it is a physical port or not).

我正在寻找诸如“isPhysical(接口)”之类的东西。(可以有多个物理lan口,我只想知道是不是物理口)。

UPDATE:

更新:

Jay and Chris thanks!

杰伊和克里斯谢谢!

(Not enough space in the comments so I'll post here)

(评论空间不够,所以我会在这里发布)

I'm currently looking at using WMI Win32_NetworkAdapter. However, it shows 4 interfaces with AdapterType="Ethernet 802.3".

我目前正在考虑使用 WMI Win32_NetworkAdapter。但是,它显示了 AdapterType="Ethernet 802.3" 的 4 个接口。

I only have 1 phyical port on my pc the others have the Name, "Virtual Machine Network Services Driver". (I assume these are installed by my company for some nefarious reason)

我的电脑上只有 1 个物理端口,其他端口的名称为“虚拟机网络服务驱动程序”。(我假设这些是我的公司出于某种邪恶原因安装的)

I found the attribute I need in the interface, but it's not available in winXP!!!

我在界面中找到了我需要的属性,但是在winXP下没有!!!

PhysicalAdapter Data type: boolean Access type: Read-only

Indicates whether the adapter is a physical or a logical adapter. If True, the adapter is physical. Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0: This property is not available.

PhysicalAdapter 数据类型:boolean 访问类型:只读

指示适配器是物理适配器还是逻辑适配器。如果为 True,则适配器是物理的。Windows Server 2003、Windows XP、Windows 2000 和 Windows NT 4.0:此属性不可用。

Is there any way to determine if it's a physical port, other than doing a text filter for the word "Virtual" in the name?

除了对名称中的“Virtual”一词进行文本过滤之外,还有什么方法可以确定它是否是物理端口?

UPDATE 12/08

更新 12/08

Looks like the virtual interfaces are added when you have a VM installed.

看起来虚拟接口是在安装 VM 时添加的。

Here's some details about the virtual adapter: http://blogs.msdn.com/virtual_pc_guy/archive/2005/04/01/404816.aspx

以下是有关虚拟适配器的一些详细信息:http: //blogs.msdn.com/virtual_pc_guy/archive/2005/04/01/404816.aspx

Found an issue where if the user doesn't have admin rights the WMI interface doesn't return the data needed. So, now looking at the GetAdapterInfo method. However, it seems to add 'virtual machine services driver" to the actual adapter's description, making the 'virtual' text check invalid.

发现一个问题,如果用户没有管理员权限,WMI 界面不会返回所需的数据。所以,现在看看 GetAdapterInfo 方法。但是,它似乎在实际适配器的描述中添加了“虚拟机服务驱动程序”,使“虚拟”文本检查无效。

And the structure returned by GetAdapterInfo:

以及 GetAdapterInfo 返回的结构:

http://msdn.microsoft.com/en-us/library/aa366062(VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa366062(VS.85).aspx

回答by Jay Hofacker

Call GetIfEntryand look for a dwType of IF_TYPE_ETHERNET_CSMACD in the MIB_IFROWstructure returned.

调用GetIfEntry并在返回的MIB_IFROW结构中查找 IF_TYPE_ETHERNET_CSMACD 的 dwType 。

Or in .NET, look for a System.Net.NetworkInformation.NetworkInterface with a NetworkInterfaceTypeof Ethernet.

或者在 .NET 中,查找具有以太网NetworkInterfaceType的System.Net.NetworkInformation.NetworkInterface 。

回答by Chris Roland

The best way I've seen so far with any system Windows 2000 SP2 and up is using WMI.

迄今为止,我在任何系统 Windows 2000 SP2 及更高版本中看到的最佳方式是使用 WMI。

http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx

Win32_NetworkAdapter http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx

Win32_NetworkAdapter http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx

And you can use the namespace System.Net.NetworkInformation Jay mentioned. Here is a link about detecting what network cards are connected or disconnected:

您可以使用 Jay 提到的命名空间 System.Net.NetworkInformation。这是有关检测连接或断开哪些网卡的链接:

http://felizk.dk/?p=43

http://felizk.dk/?p=43

回答by monkut

Check interface via: Call GetIfEntry and look for a dwType of IF_TYPE_ETHERNET_CSMACD in the MIB_IFROW structure returned.

检查接口通过:调用 GetIfEntry 并在返回的 MIB_IFROW 结构中查找 IF_TYPE_ETHERNET_CSMACD 的 dwType。

Or in .NET, look for a System.Net.NetworkInformation.NetworkInterface with a NetworkInterfaceType of Ethernet.

或者在 .NET 中,查找具有以太网 NetworkInterfaceType 的 System.Net.NetworkInformation.NetworkInterface。

And check if the name contains the text, "Virtual".

并检查名称是否包含文本“Virtual”。

---This seems like a lame way to check, but so far it's the only method I see to ignore those virtual 802.3 adapters.

---这似乎是一种蹩脚的检查方法,但到目前为止,这是我看到的唯一一种忽略那些虚拟 802.3 适配器的方法。

回答by Jeff

If a machine has two NICs, how will you pick which one is the right one? The only way I've seen this done is giving the user a dropdown combo with each interface's IP address. See Wireshark for an example -

如果一台机器有两个 NIC,你将如何选择哪一个是正确的?我见过这样做的唯一方法是为用户提供一个下拉组合,其中包含每个接口的 IP 地址。以 Wireshark 为例 -

If there are other solutions, I'd like to hear them -

如果有其他解决方案,我想听听他们 -

回答by RodeoClown

I'm not sure of the answer - but be aware that it is possible to have multiple ethernet interfaces.

我不确定答案 - 但请注意可能有多个以太网接口。