How can I get the index of an interace for a connection in C#?
In case index isn't a standard term, I mean the number associated with an interface, such as when you use the command "netsh int ipv4 show int" to list your connections by index on the left. It's also used in "route add [gateway] mask [index] if [interface index]".
I know the name and description of the interface, so it's fairly straightfoward to use NetworkInterface.GetAllNetworkInterfaces() and then find the right one there. From there though, I can't find the index. I thought ID might be the same, but that has values of the form "{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", not small integer values.
All network interfaces are given a GUID which uniquely sets them apart and is used for individual manipulation in the Win environment that's what ID is.
The idx isn't directly assigned to the NIC but its used by the Windows Management System as a user firendly way of setting details .
Theres a snippet of code here which shows how to access the ManagementObjectSearcher which may contain the information, you would have to parse through the results (instead of just selecting name as below)
I think you want
As in
Note that
GetIPv4Properties()
can returnnull
if no IPv4 information is available for the interface.This msdn page shows an example that might be helpful.
Are you talking about the network adapter binding order? If so, that is normally stored in this registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Linkage