using System; using System.Net.NetworkInformation; using System.Runtime.InteropServices; namespace SBRL.Utilities { public static class NetTools { /// /// case-insensitively gets the IPv4 index of the network interface with the given name. /// /// The interface name to search for. /// The IPv4 index of the network interface with the given name. public static int GetNetworkIndexByName4(string targetInterfaceName) { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface nic in nics) { Console.WriteLine("Id: {0}, Description: {1}", nic.Id, nic.Description); IPInterfaceProperties ipProps = nic.GetIPProperties(); IPv4InterfaceProperties ip4Props = ipProps.GetIPv4Properties(); if (nic.Id == targetInterfaceName) return ip4Props.Index; } throw new Exception($"Error: Can't find network interface with the name {targetInterfaceName}."); } } }