Now I am trying to get network adapter in use among of several network adapters on my machine so that I can get ip address, mac address and adapter name of the network adapter. But I am not having any luck. What I found the solution for it is Get current network adapter in use. I dont think it is for .Net framework project but ASP.NET Core.
My code is below.
public void GetIpAndMacAddress()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
// Only consider Ethernet network interfaces
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
nic.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties adapterProperties = nic.GetIPProperties();
foreach (var ip in adapterProperties.UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
IPAddress = ip.Address.ToString();
}
string hexMac = nic.GetPhysicalAddress().ToString(),
regex = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})",
replace = "$1:$2:$3:$4:$5:$6";
IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
netAdapterName = nic.Name;
MACAddress = Regex.Replace(hexMac, regex, replace);
return;
}
}
}