30 lines
1021 B
C#
30 lines
1021 B
C#
using System;
|
|
using System.Net.NetworkInformation;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace SBRL.Utilities
|
|
{
|
|
public static class NetTools
|
|
{
|
|
/// <summary>
|
|
/// case-insensitively gets the IPv4 index of the network interface with the given name.
|
|
/// </summary>
|
|
/// <param name="interfaceName">The interface name to search for.</param>
|
|
/// <returns>The IPv4 index of the network interface with the given name.</returns>
|
|
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}.");
|
|
}
|
|
}
|
|
}
|
|
|