SddpViewer/NetworkAdapter.cs

32 lines
1014 B
C#

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace SddpViewer;
public record NetworkAdapter(string Name, IPAddress IpAddress)
{
public string DisplayName => $"{Name} - {IpAddress}";
public static IEnumerable<NetworkAdapter> GetAvailableNetworkAdapter()
{
foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus != OperationalStatus.Up)
continue;
if (nic.NetworkInterfaceType == NetworkInterfaceType.Loopback)
continue;
var physicalAddress = nic.GetIPProperties()
.UnicastAddresses.FirstOrDefault(
x => x.Address.AddressFamily == AddressFamily.InterNetwork
);
if (physicalAddress is not null)
{
yield return new NetworkAdapter(nic.Name, physicalAddress.Address);
}
}
}
}