How to get the network adapter index?

From the code I want to force a Windows computer to use a specific network adapter for all connections to a specific IP address.

I plan to do this using the ROUTE ADD command line tool, but this requires that I know the index number of the network adapters in advance (as should be indicated by the ROUTE ADD command).

QUESTION: How can I programmatically get the index of a network adapter, given that I know its name?

I know that ROUTE PRINT shows me the information I need (index numbers of all network adapters are present), but should there also be a way to get this information programmatically too (C #)?

Please note that I do not like parsing text output from ROUTE PRINT, as the text format can change in different versions of Windows.

+8
c # windows networking routing
source share
2 answers

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

Here is a sample code:

 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 the network adapter:

 PrintInterfaceIndex("your network adapter name"); 

You can also get the InterfaceIndex of your network adapter using the Win32_NetworkAdapter WMI class. The Win32_NetworkAdapter class contains the InterfaceIndex property.

So, to get InterfaceIndex for the given network adapter, enter 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 can also use the Win32 API function GetAdaptersInfo in combination with the IP_ADAPTER_INFO construct. Here you will find an example of pinvoke.net .

+7
source share

Have you learned using the C # system.net.networkinformation interface?

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

I am not familiar with ROUTE ADD, but you can theoretically marry information from one to the other.

0
source share

All Articles