using Masuit.Tools.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
namespace Masuit.Tools.Hardware
{
///
/// 硬件信息,部分功能需要C++支持
///
public static partial class SystemInfo
{
#region 字段
private const int GwHwndfirst = 0;
private const int GwHwndnext = 2;
private const int GwlStyle = -16;
private const int WsVisible = 268435456;
private const int WsBorder = 8388608;
private static readonly PerformanceCounter PcCpuLoad; //CPU计数器
private static readonly PerformanceCounter MemoryCounter = new PerformanceCounter();
private static readonly PerformanceCounter CpuCounter = new PerformanceCounter();
private static readonly PerformanceCounter DiskReadCounter = new PerformanceCounter();
private static readonly PerformanceCounter DiskWriteCounter = new PerformanceCounter();
private static readonly string[] InstanceNames;
private static readonly PerformanceCounter[] NetRecvCounters;
private static readonly PerformanceCounter[] NetSentCounters;
#endregion
#region 构造函数
///
/// 静态构造函数
///
static SystemInfo()
{
//初始化CPU计数器
PcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total")
{
MachineName = "."
};
PcCpuLoad.NextValue();
//CPU个数
ProcessorCount = Environment.ProcessorCount;
//获得物理内存
try
{
using var mc = new ManagementClass("Win32_ComputerSystem");
using var moc = mc.GetInstances();
foreach (var mo in moc)
{
if (mo["TotalPhysicalMemory"] != null)
{
PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
}
}
var cat = new PerformanceCounterCategory("Network Interface");
InstanceNames = cat.GetInstanceNames();
NetRecvCounters = new PerformanceCounter[InstanceNames.Length];
NetSentCounters = new PerformanceCounter[InstanceNames.Length];
for (int i = 0; i < InstanceNames.Length; i++)
{
NetRecvCounters[i] = new PerformanceCounter();
NetSentCounters[i] = new PerformanceCounter();
}
CompactFormat = false;
}
catch (Exception e)
{
LogManager.Error(e);
}
}
#endregion
private static bool CompactFormat { get; set; }
#region CPU核心
///
/// 获取CPU核心数
///
public static int ProcessorCount { get; }
#endregion
#region CPU占用率
///
/// 获取CPU占用率 %
///
public static float CpuLoad => PcCpuLoad.NextValue();
#endregion
#region 可用内存
///
/// 获取可用内存
///
public static long MemoryAvailable
{
get
{
try
{
using var mos = new ManagementClass("Win32_OperatingSystem");
foreach (var mo in mos.GetInstances())
{
if (mo["FreePhysicalMemory"] != null)
{
return 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
}
}
return 0;
}
catch (Exception)
{
return 0;
}
}
}
#endregion
#region 物理内存
///
/// 获取物理内存
///
public static long PhysicalMemory { get; }
#endregion
#region 查找所有应用程序标题
///
/// 查找所有应用程序标题
///
/// 应用程序标题范型
/// 所有应用程序集合
public static ArrayList FindAllApps(int handle)
{
var apps = new ArrayList();
int hwCurr = GetWindow(handle, GwHwndfirst);
while (hwCurr > 0)
{
int IsTask = WsVisible | WsBorder;
int lngStyle = GetWindowLongA(hwCurr, GwlStyle);
bool taskWindow = (lngStyle & IsTask) == IsTask;
if (taskWindow)
{
int length = GetWindowTextLength(new IntPtr(hwCurr));
StringBuilder sb = new StringBuilder(2 * length + 1);
GetWindowText(hwCurr, sb, sb.Capacity);
string strTitle = sb.ToString();
if (!string.IsNullOrEmpty(strTitle))
{
apps.Add(strTitle);
}
}
hwCurr = GetWindow(hwCurr, GwHwndnext);
}
return apps;
}
#endregion
#region 获取CPU的数量
///
/// 获取CPU的数量
///
/// CPU的数量
public static int GetCpuCount()
{
try
{
using var m = new ManagementClass("Win32_Processor");
using var mn = m.GetInstances();
return mn.Count;
}
catch (Exception)
{
return 0;
}
}
#endregion
#region 获取CPU信息
private static List _cpuObjects;
///
/// 获取CPU信息
///
/// CPU信息
public static List GetCpuInfo()
{
try
{
using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
using var moc = mos.Get();
_cpuObjects ??= moc.AsParallel().Cast().ToList();
return _cpuObjects.Select(mo => new CpuInfo
{
CpuLoad = CpuLoad,
NumberOfLogicalProcessors = ProcessorCount,
CurrentClockSpeed = mo.Properties["CurrentClockSpeed"].Value.ToString(),
Manufacturer = mo.Properties["Manufacturer"].Value.ToString(),
MaxClockSpeed = mo.Properties["MaxClockSpeed"].Value.ToString(),
Type = mo.Properties["Name"].Value.ToString(),
DataWidth = mo.Properties["DataWidth"].Value.ToString(),
DeviceID = mo.Properties["DeviceID"].Value.ToString(),
NumberOfCores = Convert.ToInt32(mo.Properties["NumberOfCores"].Value),
Temperature = GetCPUTemperature()
}).ToList();
}
catch (Exception)
{
return new List();
}
}
#endregion
#region 获取内存信息
///
/// 获取内存信息
///
/// 内存信息
public static RamInfo GetRamInfo()
{
var info = new RamInfo
{
MemoryAvailable = GetFreePhysicalMemory(),
PhysicalMemory = GetTotalPhysicalMemory(),
TotalPageFile = GetTotalVirtualMemory(),
AvailablePageFile = GetTotalVirtualMemory() - GetUsedVirtualMemory(),
AvailableVirtual = 1 - GetUsageVirtualMemory(),
TotalVirtual = 1 - GetUsedPhysicalMemory()
};
return info;
}
#endregion
#region 获取CPU温度
///
/// 获取CPU温度
///
/// CPU温度
public static float GetCPUTemperature()
{
try
{
string str = "";
using var mos = new ManagementObjectSearcher(@"root\WMI", "select * from MSAcpi_ThermalZoneTemperature");
var moc = mos.Get();
foreach (var mo in moc)
{
str += mo.Properties["CurrentTemperature"].Value.ToString();
}
//这就是CPU的温度了
float temp = (float.Parse(str) - 2732) / 10;
return (float)Math.Round(temp, 2);
}
catch (Exception)
{
return 0;
}
}
#endregion
#region WMI接口获取CPU使用率
///
/// WMI接口获取CPU使用率
///
///
public static string GetProcessorData()
{
float d = GetCounterValue(CpuCounter, "Processor", "% Processor Time", "_Total");
return CompactFormat ? (int)d + "%" : d.ToString("F") + "%";
}
#endregion
#region 获取虚拟内存使用率详情
///
/// 获取虚拟内存使用率详情
///
///
public static string GetMemoryVData()
{
float d = GetCounterValue(MemoryCounter, "Memory", "% Committed Bytes In Use", null);
var str = d.ToString("F") + "% (";
d = GetCounterValue(MemoryCounter, "Memory", "Committed Bytes", null);
str += FormatBytes(d) + " / ";
d = GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
return str + FormatBytes(d) + ") ";
}
///
/// 获取虚拟内存使用率
///
///
public static float GetUsageVirtualMemory()
{
return GetCounterValue(MemoryCounter, "Memory", "% Committed Bytes In Use", null);
}
///
/// 获取虚拟内存已用大小
///
///
public static float GetUsedVirtualMemory()
{
return GetCounterValue(MemoryCounter, "Memory", "Committed Bytes", null);
}
///
/// 获取虚拟内存总大小
///
///
public static float GetTotalVirtualMemory()
{
return GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
}
#endregion
#region 获取物理内存使用率详情
///
/// 获取物理内存使用率详情描述
///
///
public static string GetMemoryPData()
{
string s = QueryComputerSystem("totalphysicalmemory");
float totalphysicalmemory = Convert.ToSingle(s);
float d = GetCounterValue(MemoryCounter, "Memory", "Available Bytes", null);
d = totalphysicalmemory - d;
s = CompactFormat ? "%" : "% (" + FormatBytes(d) + " / " + FormatBytes(totalphysicalmemory) + ")";
d /= totalphysicalmemory;
d *= 100;
return CompactFormat ? (int)d + s : d.ToString("F") + s;
}
///
/// 获取物理内存总数,单位B
///
///
public static float GetTotalPhysicalMemory()
{
string s = QueryComputerSystem("totalphysicalmemory");
return s.TryConvertTo();
}
///
/// 获取空闲的物理内存数,单位B
///
///
public static float GetFreePhysicalMemory()
{
return GetCounterValue(MemoryCounter, "Memory", "Available Bytes", null);
}
///
/// 获取已经使用了的物理内存数,单位B
///
///
public static float GetUsedPhysicalMemory()
{
return GetTotalPhysicalMemory() - GetFreePhysicalMemory();
}
#endregion
#region 获取硬盘的读写速率
///
/// 获取硬盘的读写速率
///
/// 读或写
///
public static float GetDiskData(DiskData dd) => dd == DiskData.Read ? GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") : dd == DiskData.Write ? GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total") : dd == DiskData.ReadAndWrite ? GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") + GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total") : 0;
#endregion
#region 获取网络的传输速率
///
/// 获取网络的传输速率
///
/// 上传或下载
///
public static float GetNetData(NetData nd)
{
if (InstanceNames.Length == 0)
{
return 0;
}
float d = 0;
for (int i = 0; i < InstanceNames.Length; i++)
{
float receied = GetCounterValue(NetRecvCounters[i], "Network Interface", "Bytes Received/sec", InstanceNames[i]);
float send = GetCounterValue(NetSentCounters[i], "Network Interface", "Bytes Sent/sec", InstanceNames[i]);
switch (nd)
{
case NetData.Received:
d += receied;
break;
case NetData.Sent:
d += send;
break;
case NetData.ReceivedAndSent:
d += receied + send;
break;
default:
d += 0;
break;
}
}
return d;
}
#endregion
///
/// 获取网卡硬件地址
///
///
public static IList GetMacAddress()
{
//获取网卡硬件地址
try
{
IList list = new List();
using var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
using var moc = mc.GetInstances();
foreach (var mo in moc)
{
if ((bool)mo["IPEnabled"])
{
list.Add(mo["MacAddress"].ToString());
}
}
return list;
}
catch (Exception)
{
return new List();
}
}
///
/// 获取当前使用的IP
///
///
public static IPAddress GetLocalUsedIP()
{
return NetworkInterface.GetAllNetworkInterfaces().OrderByDescending(c => c.Speed).Where(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up).SelectMany(n => n.GetIPProperties().UnicastAddresses.Select(u => u.Address)).FirstOrDefault();
}
///
/// 获取本机所有的ip地址
///
///
public static List GetLocalIPs()
{
var interfaces = NetworkInterface.GetAllNetworkInterfaces().OrderByDescending(c => c.Speed).Where(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up); //所有网卡信息
return interfaces.SelectMany(n => n.GetIPProperties().UnicastAddresses).ToList();
}
#region 将速度值格式化成字节单位
///
/// 将速度值格式化成字节单位
///
///
///
public static string FormatBytes(this double bytes)
{
int unit = 0;
while (bytes > 1024)
{
bytes /= 1024;
++unit;
}
string s = CompactFormat ? ((int)bytes).ToString() : bytes.ToString("F") + " ";
return s + (Unit)unit;
}
#endregion
#region 查询计算机系统信息
///
/// 获取计算机开机时间
///
/// datetime
public static DateTime BootTime()
{
var query = new SelectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem WHERE Primary='true'");
using var searcher = new ManagementObjectSearcher(query);
foreach (var mo in searcher.Get())
{
return ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
}
return DateTime.Now - TimeSpan.FromMilliseconds(Environment.TickCount & int.MaxValue);
}
///
/// 查询计算机系统信息
///
/// 类型名
///
public static string QueryComputerSystem(string type)
{
try
{
string str = null;
var mos = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
foreach (var mo in mos.Get())
{
str = mo[type].ToString();
}
return str;
}
catch (Exception e)
{
return "未能获取到当前计算机系统信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。异常信息:" + e.Message;
}
}
#endregion
#region 获取环境变量
///
/// 获取环境变量
///
/// 环境变量名
///
public static string QueryEnvironment(string type) => Environment.ExpandEnvironmentVariables(type);
#endregion
#region 获取磁盘空间
///
/// 获取磁盘可用空间
///
///
public static Dictionary DiskFree()
{
try
{
var dic = new Dictionary();
var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
foreach (var mo in mos.Get())
{
if (null != mo["DeviceID"] && null != mo["FreeSpace"])
{
dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["FreeSpace"].ToString())));
}
}
return dic;
}
catch (Exception)
{
return new Dictionary()
{
{ "null", "未能获取到当前计算机的磁盘信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。" }
};
}
}
///
/// 获取磁盘总空间
///
///
public static Dictionary DiskTotalSpace()
{
try
{
var dic = new Dictionary();
using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
foreach (var mo in mos.Get())
{
if (null != mo["DeviceID"] && null != mo["Size"])
{
dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["Size"].ToString())));
}
}
return dic;
}
catch (Exception)
{
return new Dictionary();
}
}
///
/// 获取磁盘已用空间
///
///
public static Dictionary DiskUsedSpace()
{
try
{
var dic = new Dictionary();
using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
foreach (var mo in mos.Get())
{
if (null != mo["DeviceID"] && null != mo["Size"])
{
var free = mo["FreeSpace"];
dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["Size"].ToString()) - free.ToString().ToDouble()));
}
}
return dic;
}
catch (Exception)
{
return new Dictionary()
{
{ "null", "未能获取到当前计算机的磁盘信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。" }
};
}
}
///
/// 获取磁盘使用率
///
///
public static Dictionary DiskUsage()
{
try
{
var dic = new Dictionary();
using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
foreach (var mo in mos.Get())
{
var device = mo["DeviceID"];
if (null != device)
{
var free = mo["FreeSpace"];
var total = mo["Size"];
if (null != total && total.ToString().TryConvertTo() > 0)
{
dic.Add(device.ToString(), 1 - free.ToString().TryConvertTo() / total.ToString().TryConvertTo());
}
}
}
return dic;
}
catch (Exception)
{
return new Dictionary()
{
{ "未能获取到当前计算机的磁盘信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。", 0 }
};
}
}
#endregion
private static float GetCounterValue(PerformanceCounter pc, string categoryName, string counterName, string instanceName)
{
pc.CategoryName = categoryName;
pc.CounterName = counterName;
pc.InstanceName = instanceName;
return pc.NextValue();
}
#region Win32API声明
#pragma warning disable 1591
[DllImport("kernel32")]
public static extern void GetWindowsDirectory(StringBuilder winDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder sysDir, int count);
[DllImport("kernel32")]
public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MemoryInfo meminfo);
[DllImport("kernel32")]
public static extern void GetSystemTime(ref SystemtimeInfo stinfo);
[DllImport("IpHlpApi.dll")]
public static extern uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
[DllImport("User32")]
public static extern int GetWindow(int hWnd, int wCmd);
[DllImport("User32")]
public static extern int GetWindowLongA(int hWnd, int wIndx);
[DllImport("user32.dll")]
public static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
[DllImport("user32", CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
#pragma warning restore 1591
#endregion
}
}