简体   繁体   中英

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.

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).

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#)?

Note, that I don't like parsing the text output from ROUTE PRINT, as the text format may change with different Windows versions.

You can obtain the interface index of your network adapter by using the .Net NetworkInterface (and related) classes.

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. The Win32_NetworkAdapter class contains a property called InterfaceIndex.

So, to retrieve the InterfaceIndex for a network adapter with a given name, use the following code:

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. You will find an example here pinvoke.net .

have you looked into using C#'s system.net.networkinformation interface?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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