You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Infrastructure/Extensions/HelperExtensions.cs

77 lines
2.8 KiB

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace Infrastructure.Extensions
{
public static class HelperExtensions
{
public static string GetMacAddress(this Helper helper)
{
var physicalAddress = NetworkInterface
.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Select(nic => nic.GetPhysicalAddress().ToString())
.FirstOrDefault(o => !string.IsNullOrEmpty(o));
return physicalAddress.ToLower();
}
public static List<IPAddress> GetLocalIPList(this Helper helper)
{
var ipList = new List<IPAddress>();
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (var ua in ni.GetIPProperties().UnicastAddresses)
{
if (ua.Address.AddressFamily == AddressFamily.InterNetwork)
{
ipList.Add(ua.Address);
}
}
}
return ipList;
}
public static IPAddress GetLocalIP(this Helper helper)
{
var ipList = new List<IPAddress>();
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (var ua in ni.GetIPProperties().UnicastAddresses)
{
if (ua.Address.AddressFamily == AddressFamily.InterNetwork)
{
ipList.Add(ua.Address);
}
}
}
var ip = ipList.Where(o => !o.ToString().StartsWith("10.") && o.ToString().StartsWith("172.") && o.ToString().StartsWith("192.") && o.ToString().StartsWith("127."))
.FirstOrDefault() ?? ipList.FirstOrDefault(o => !o.ToString().StartsWith("192.168."))
?? ipList.FirstOrDefault(o => !o.ToString().StartsWith("192."))
?? ipList.FirstOrDefault();
return ip;
}
public static string GetRunTime(this Helper helper)
{
var os = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
os += "windows";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
os += "linux";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
os += "osx";
}
os += $"-{RuntimeInformation.OSArchitecture.ToString().ToLower()}";
return os;
}
}
}