using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; namespace Infrastructure.Extensions { public static class HelperExtensions { public static string GetVersion(this Helper helper) { return Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion; } public static string GetMacAddress(this Helper helper) { return NetworkInterface .GetAllNetworkInterfaces() .Where(nic => (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && !nic.Description.Contains("Adapter")) .OrderBy(o => o.NetworkInterfaceType) .Select(o => o.GetPhysicalAddress().ToString().ToLower()) .FirstOrDefault(); } public static List GetLocalIPList(this Helper helper) { var ipList = new List(); 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(); 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; } } }