How to get network adapter index?

10,649

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.

Share:
10,649

Related videos on Youtube

Martin Christiansen
Author by

Martin Christiansen

Software developer at Emerson Transportation Solutions, Denmark.

Updated on September 14, 2022

Comments

  • Martin Christiansen
    Martin Christiansen over 1 year

    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.

  • Martin Christiansen
    Martin Christiansen almost 12 years
    Yes I have, but it doesn't seem to give me the information I need. Try for yourself to enter ROUTE PRINT from a command line and look at the first table displayed. The index numbers I need are in the leftmost column (each line lists <index number>...<MAC address>...<Adapter Name>).
  • Martin Christiansen
    Martin Christiansen almost 12 years
    Hi Hans - your code looks very promising - I will try it out tomorrow as first thing in the morning, when I get back on work.
  • Martin Christiansen
    Martin Christiansen almost 12 years
    Hi Hans - your code looks very promising, I will try it out tomorrow as first thing when I get back on work. However, earlier today I tried using the WMI approach, and I listed all properties available for each network adapter, but I did not see any propterty named "InterfaceIndex" - maybe I just overlooked it? I'm using Windows XP Professional SP3. I'll let you know what I find out tomorrow. Thank you very much for your involvement, it's fantastic to have people like you around!
  • Hans
    Hans almost 12 years
    @MartinChristiansen: This WMI property is not available under Windows XP.
  • Martin Christiansen
    Martin Christiansen almost 12 years
    That explains why I did not see it. However, I have now tried your first code example, and it works like a charm - even with XP. You certainly saved my day - thanks al lot!