简体   繁体   English

如何获得网络适配器索引?

[英]How to get network adapter index?

From code I want to force a Windows machine to use a specific network adapter for all connections to a specific IP address. 从代码我想强制Windows机器使用特定的网络适配器进行到特定IP地址的所有连接。

I plan to do so by using the ROUTE ADD command line tool, but this requires that I know in advance the network adapters' index number (as it must be given to the ROUTE ADD command). 我计划通过使用ROUTE ADD命令行工具来实现,但这要求我事先知道网络适配器的索引号 (因为它必须提供给ROUTE ADD命令)。

QUESTION: How can I programmatically retrieve a network adapter's index, given I know its name? 问题:如果我知道它的名字,我如何以编程方式检索网络适配器的索引?

I'm aware that ROUTE PRINT shows me the information I need (the index numbers of all network adapters present), but there must be a way to get that information programmatically too (C#)? 我知道ROUTE PRINT向我显示了我需要的信息(所有网络适配器的索引号),但是必须有一种方法来以编程方式获取该信息(C#)?

Note, that I don't like parsing the text output from ROUTE PRINT, as the text format may change with different Windows versions. 请注意,我不喜欢解析ROUTE PRINT的文本输出,因为文本格式可能会随着不同的Windows版本而改变。

You can obtain the interface index of your network adapter by using the .Net NetworkInterface (and related) classes. 您可以使用.Net NetworkInterface (和相关)类获取网络适配器的接口索引。

Here is a code example: 这是一个代码示例:

static void PrintInterfaceIndex(string adapterName)
{
  NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();

  Console.WriteLine("IPv4 interface information for {0}.{1}",
                properties.HostName, properties.DomainName);


  foreach (NetworkInterface adapter in nics)
  {               
    if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false)
    {
      continue;
    }

    if (!adapter.Description.Equals(adapterName, StringComparison.OrdinalIgnoreCase))
    {
      continue;
    }
    Console.WriteLine(adapter.Description);                                
    IPInterfaceProperties adapterProperties = adapter.GetIPProperties();                
    IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
    if (p == null)
    {
      Console.WriteLine("No information is available for this interface.");                    
      continue;
    }                
    Console.WriteLine("  Index : {0}", p.Index);              
  }
}

Then just call this function with the name of your network adapter: 然后只需使用网络适配器的名称调用此函数:

PrintInterfaceIndex("your network adapter name");

You can also obtain the InterfaceIndex of your network adapter by using the Win32_NetworkAdapter WMI class. 您还可以使用Win32_NetworkAdapter WMI类获取网络适配器的InterfaceIndex。 The Win32_NetworkAdapter class contains a property called InterfaceIndex. Win32_NetworkAdapter类包含一个名为InterfaceIndex的属性。

So, to retrieve the InterfaceIndex for a network adapter with a given name, use the following code: 因此,要检索具有给定名称的网络适配器的InterfaceIndex,请使用以下代码:

ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");

ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE Description='<Your Network Adapter name goes here>'");           
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
{
  using (ManagementObjectCollection queryCollection = searcher.Get())
  {             
    foreach (ManagementObject mo in queryCollection)
    {                 
      Console.WriteLine("InterfaceIndex : {0}, name {1}", mo["InterfaceIndex"], mo["Description"]);
    }
  }
}

If you do not want to use WMI you could also use the Win32 API function GetAdaptersInfo in combination with the IP_ADAPTER_INFO struct. 如果您不想使用WMI,您还可以将Win32 API函数GetAdaptersInfoIP_ADAPTER_INFO结构一起使用。 You will find an example here pinvoke.net . 你会在这里找到一个例子pinvoke.net

have you looked into using C#'s system.net.networkinformation interface? 您是否考虑过使用C#的system.net.networkinformation接口?

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx

I'm not familiar with ROUTE ADD, but you can theoretically marry up information from one with the other. 我不熟悉ROUTE ADD,但理论上你可以将信息与其他信息结合起来。

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

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