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.
111 lines
4.0 KiB
111 lines
4.0 KiB
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Management;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Infrastructure.Extensions
|
|
{
|
|
public static class HelperExtensions
|
|
{
|
|
public static string GetVersion(this Helper helper)
|
|
{
|
|
return Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
|
|
}
|
|
|
|
public static string GetCPUNumber(this Helper helper)
|
|
{
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
{
|
|
//bash:cat /proc/cpuinfo
|
|
return Regex.Match(File.ReadAllText("/proc/cpuinfo"), @"Serial\s*:\s*([^\s]+)").Groups[1].Value;
|
|
}
|
|
else
|
|
{
|
|
//cmd:WMic CPU get PRocessorID
|
|
string cpuID = string.Empty;
|
|
var mc = new ManagementClass("win32_processor");
|
|
var moc = mc.GetInstances();
|
|
|
|
foreach (ManagementObject mo in moc)
|
|
{
|
|
if (cpuID == "")
|
|
{
|
|
cpuID = mo.Properties["processorID"].Value.ToString();
|
|
}
|
|
}
|
|
return cpuID;
|
|
}
|
|
}
|
|
|
|
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<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;
|
|
}
|
|
}
|
|
} |