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.
93 lines
3.5 KiB
93 lines
3.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
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<AssemblyInformationalVersionAttribute>().InformationalVersion;
|
|
}
|
|
|
|
public static string GetAssemblyVersion(this Helper helper)
|
|
{
|
|
return Assembly.GetEntryAssembly().GetName().Version?.ToString();
|
|
}
|
|
|
|
public static string GetMacAddress(this Helper helper)
|
|
{
|
|
return NetworkInterface
|
|
.GetAllNetworkInterfaces()
|
|
.Where(nic => (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && !nic.Description.Contains("Adapter", StringComparison.Ordinal))
|
|
.OrderBy(o => o.NetworkInterfaceType)
|
|
.Select(o => o.GetPhysicalAddress().ToString().ToLower(CultureInfo.InvariantCulture))
|
|
.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.", StringComparison.Ordinal) &&
|
|
o.ToString().StartsWith("172.", StringComparison.Ordinal) &&
|
|
o.ToString().StartsWith("192.", StringComparison.Ordinal) &&
|
|
o.ToString().StartsWith("127.", StringComparison.Ordinal)).FirstOrDefault()
|
|
?? ipList.FirstOrDefault(o => !o.ToString().StartsWith("192.168.", StringComparison.Ordinal))
|
|
?? ipList.FirstOrDefault(o => !o.ToString().StartsWith("192.", StringComparison.Ordinal))
|
|
?? 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(CultureInfo.InvariantCulture)}";
|
|
return os;
|
|
}
|
|
}
|
|
} |