Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 705
Martin Christiansen Avatar asked Jun 21 '12 18:06

Martin Christiansen


People also ask

How do I find my WIFI adapter index number?

You can find all of your computer's network interface index numbers using the "route" command in the command prompt. Network interface index numbers differ from Internet Protocol addresses in that they are used within a single device to direct traffic, while IP addresses regulate inter-device communication.

What is a network adapter index?

This index value identifies the network connection on an extensible switch port. The index value is unique for each network adapter connection to a port. Although most network adapters require only one index value, the port connection to the external network adapter may be assigned multiple index values.

How do I find my network adapter details?

Click Start, click Run, type cmd, and then click OK. At the command prompt, type ipconfig, and then press ENTER. Note the following information: The IP address of the network adapter that you want to check.


1 Answers

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.

like image 196
Hans Avatar answered Sep 22 '22 04:09

Hans