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 .
Hans
source share