|
|
@@ -30,15 +30,12 @@ namespace Masuit.Tools.Hardware
|
|
|
private const int WsBorder = 8388608;
|
|
|
private static readonly PerformanceCounter PcCpuLoad; //CPU计数器
|
|
|
|
|
|
- private static readonly PerformanceCounter MemoryCounter;
|
|
|
- private static readonly PerformanceCounter CpuCounter;
|
|
|
- private static readonly PerformanceCounter DiskReadCounter;
|
|
|
- private static readonly PerformanceCounter DiskWriteCounter;
|
|
|
+ private static readonly PerformanceCounter IOCounter;
|
|
|
|
|
|
private static readonly string[] InstanceNames = { };
|
|
|
private static readonly PerformanceCounter[] NetRecvCounters;
|
|
|
private static readonly PerformanceCounter[] NetSentCounters;
|
|
|
- private static readonly Dictionary<string, dynamic> _cache = new();
|
|
|
+ private static readonly Dictionary<string, dynamic> Cache = new();
|
|
|
|
|
|
public static bool IsWinPlatform => Environment.OSVersion.Platform is PlatformID.Win32Windows
|
|
|
or PlatformID.Win32S or PlatformID.WinCE or PlatformID.Win32NT;
|
|
|
@@ -54,10 +51,7 @@ namespace Masuit.Tools.Hardware
|
|
|
{
|
|
|
if (IsWinPlatform)
|
|
|
{
|
|
|
- MemoryCounter = new PerformanceCounter();
|
|
|
- CpuCounter = new PerformanceCounter();
|
|
|
- DiskReadCounter = new PerformanceCounter();
|
|
|
- DiskWriteCounter = new PerformanceCounter();
|
|
|
+ IOCounter = new PerformanceCounter();
|
|
|
|
|
|
//初始化CPU计数器
|
|
|
PcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total")
|
|
|
@@ -144,7 +138,7 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static string GetProcessorData()
|
|
|
{
|
|
|
- var d = GetCounterValue(CpuCounter, "Processor", "% Processor Time", "_Total");
|
|
|
+ var d = GetCounterValue(IOCounter, "Processor", "% Processor Time", "_Total");
|
|
|
return CompactFormat ? (int)d + "%" : d.ToString("F") + "%";
|
|
|
}
|
|
|
|
|
|
@@ -186,7 +180,7 @@ namespace Masuit.Tools.Hardware
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- return _cache.GetOrAdd(nameof(GetCpuCount), () =>
|
|
|
+ return Cache.GetOrAdd(nameof(GetCpuCount), () =>
|
|
|
{
|
|
|
if (!IsWinPlatform)
|
|
|
{
|
|
|
@@ -281,6 +275,16 @@ namespace Masuit.Tools.Hardware
|
|
|
/// </summary>
|
|
|
public static long PhysicalMemory { get; }
|
|
|
|
|
|
+ public static long CurrentProcessMemory
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (!IsWinPlatform) return 0;
|
|
|
+ using var process = Process.GetCurrentProcess();
|
|
|
+ return (long)GetCounterValue(IOCounter, "Process", "Working Set - Private", process.ProcessName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取内存信息
|
|
|
/// </summary>
|
|
|
@@ -304,11 +308,12 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static string GetMemoryVData()
|
|
|
{
|
|
|
- float d = GetCounterValue(MemoryCounter, "Memory", "% Committed Bytes In Use", null);
|
|
|
+ if (!IsWinPlatform) return "";
|
|
|
+ float d = GetCounterValue(IOCounter, "Memory", "% Committed Bytes In Use", null);
|
|
|
var str = d.ToString("F") + "% (";
|
|
|
- d = GetCounterValue(MemoryCounter, "Memory", "Committed Bytes", null);
|
|
|
+ d = GetCounterValue(IOCounter, "Memory", "Committed Bytes", null);
|
|
|
str += FormatBytes(d) + " / ";
|
|
|
- d = GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
|
|
|
+ d = GetCounterValue(IOCounter, "Memory", "Commit Limit", null);
|
|
|
return str + FormatBytes(d) + ") ";
|
|
|
}
|
|
|
|
|
|
@@ -318,7 +323,8 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static float GetUsageVirtualMemory()
|
|
|
{
|
|
|
- return GetCounterValue(MemoryCounter, "Memory", "% Committed Bytes In Use", null);
|
|
|
+ if (!IsWinPlatform) return 0;
|
|
|
+ return GetCounterValue(IOCounter, "Memory", "% Committed Bytes In Use", null);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -327,7 +333,8 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static float GetUsedVirtualMemory()
|
|
|
{
|
|
|
- return GetCounterValue(MemoryCounter, "Memory", "Committed Bytes", null);
|
|
|
+ if (!IsWinPlatform) return 0;
|
|
|
+ return GetCounterValue(IOCounter, "Memory", "Committed Bytes", null);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -336,7 +343,8 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static float GetTotalVirtualMemory()
|
|
|
{
|
|
|
- return GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
|
|
|
+ if (!IsWinPlatform) return 0;
|
|
|
+ return GetCounterValue(IOCounter, "Memory", "Commit Limit", null);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -345,11 +353,12 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static string GetMemoryPData()
|
|
|
{
|
|
|
+ if (!IsWinPlatform) return "";
|
|
|
string s = QueryComputerSystem("totalphysicalmemory");
|
|
|
if (string.IsNullOrEmpty(s)) return "";
|
|
|
|
|
|
var totalphysicalmemory = Convert.ToSingle(s);
|
|
|
- var d = GetCounterValue(MemoryCounter, "Memory", "Available Bytes", null);
|
|
|
+ var d = GetCounterValue(IOCounter, "Memory", "Available Bytes", null);
|
|
|
d = totalphysicalmemory - d;
|
|
|
s = CompactFormat ? "%" : "% (" + FormatBytes(d) + " / " + FormatBytes(totalphysicalmemory) + ")";
|
|
|
d /= totalphysicalmemory;
|
|
|
@@ -363,7 +372,7 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static float GetTotalPhysicalMemory()
|
|
|
{
|
|
|
- return _cache.GetOrAdd(nameof(GetTotalPhysicalMemory), () =>
|
|
|
+ return Cache.GetOrAdd(nameof(GetTotalPhysicalMemory), () =>
|
|
|
{
|
|
|
var s = QueryComputerSystem("totalphysicalmemory");
|
|
|
return s.TryConvertTo<float>();
|
|
|
@@ -376,7 +385,8 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static float GetFreePhysicalMemory()
|
|
|
{
|
|
|
- return GetCounterValue(MemoryCounter, "Memory", "Available Bytes", null);
|
|
|
+ if (!IsWinPlatform) return 0;
|
|
|
+ return GetCounterValue(IOCounter, "Memory", "Available Bytes", null);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -397,15 +407,19 @@ namespace Masuit.Tools.Hardware
|
|
|
/// </summary>
|
|
|
/// <param name="dd">读或写</param>
|
|
|
/// <returns></returns>
|
|
|
- public static float GetDiskData(DiskData dd) => dd switch
|
|
|
+ public static float GetDiskData(DiskData dd)
|
|
|
{
|
|
|
- DiskData.Read => GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total"),
|
|
|
- DiskData.Write => GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total"),
|
|
|
- DiskData.ReadAndWrite => GetCounterValue(DiskReadCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") + GetCounterValue(DiskWriteCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total"),
|
|
|
- _ => 0
|
|
|
- };
|
|
|
+ if (!IsWinPlatform) return 0;
|
|
|
+ return dd switch
|
|
|
+ {
|
|
|
+ DiskData.Read => GetCounterValue(IOCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total"),
|
|
|
+ DiskData.Write => GetCounterValue(IOCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total"),
|
|
|
+ DiskData.ReadAndWrite => GetCounterValue(IOCounter, "PhysicalDisk", "Disk Read Bytes/sec", "_Total") + GetCounterValue(IOCounter, "PhysicalDisk", "Disk Write Bytes/sec", "_Total"),
|
|
|
+ _ => 0
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
- private static readonly List<DiskInfo> DiskInfos = new();
|
|
|
+ private static readonly List<DiskInfo> DiskInfos = [];
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取磁盘可用空间
|
|
|
@@ -454,6 +468,7 @@ namespace Masuit.Tools.Hardware
|
|
|
/// <returns></returns>
|
|
|
public static float GetNetData(NetData nd)
|
|
|
{
|
|
|
+ if (!IsWinPlatform) return 0;
|
|
|
if (InstanceNames is { Length: 0 }) return 0;
|
|
|
|
|
|
float d = 0;
|
|
|
@@ -495,7 +510,7 @@ namespace Masuit.Tools.Hardware
|
|
|
{
|
|
|
if (!IsWinPlatform) return new List<string>();
|
|
|
|
|
|
- return _cache.GetOrAdd(nameof(GetMacAddress), () =>
|
|
|
+ return Cache.GetOrAdd(nameof(GetMacAddress), () =>
|
|
|
{
|
|
|
IList<string> list = new List<string>();
|
|
|
using var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
|
|
@@ -530,7 +545,7 @@ namespace Masuit.Tools.Hardware
|
|
|
{
|
|
|
if (!IsWinPlatform) return "";
|
|
|
|
|
|
- return _cache.GetOrAdd(nameof(GetIPAddressWMI), () =>
|
|
|
+ return Cache.GetOrAdd(nameof(GetIPAddressWMI), () =>
|
|
|
{
|
|
|
using var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
|
|
using var moc = mc.GetInstances();
|
|
|
@@ -592,7 +607,7 @@ namespace Masuit.Tools.Hardware
|
|
|
{
|
|
|
if (!IsWinPlatform) return "";
|
|
|
|
|
|
- return _cache.GetOrAdd(nameof(GetNetworkCardAddress), () =>
|
|
|
+ return Cache.GetOrAdd(nameof(GetNetworkCardAddress), () =>
|
|
|
{
|
|
|
using var mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapter where ((MACAddress Is Not NULL) and (Manufacturer <> 'Microsoft'))");
|
|
|
using var moc = mos.Get();
|
|
|
@@ -707,7 +722,7 @@ namespace Masuit.Tools.Hardware
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- return _cache.GetOrAdd(nameof(GetSystemType), () =>
|
|
|
+ return Cache.GetOrAdd(nameof(GetSystemType), () =>
|
|
|
{
|
|
|
if (!IsWinPlatform)
|
|
|
{
|
|
|
@@ -745,7 +760,7 @@ namespace Masuit.Tools.Hardware
|
|
|
{
|
|
|
if (!IsWinPlatform) return "";
|
|
|
|
|
|
- return _cache.GetOrAdd(nameof(GetBiosSerialNumber), () =>
|
|
|
+ return Cache.GetOrAdd(nameof(GetBiosSerialNumber), () =>
|
|
|
{
|
|
|
using var searcher = new ManagementObjectSearcher("select * from Win32_BIOS");
|
|
|
using var mos = searcher.Get();
|
|
|
@@ -772,7 +787,7 @@ namespace Masuit.Tools.Hardware
|
|
|
{
|
|
|
if (!IsWinPlatform) return new BiosInfo();
|
|
|
|
|
|
- return _cache.GetOrAdd(nameof(GetBiosInfo), () =>
|
|
|
+ return Cache.GetOrAdd(nameof(GetBiosInfo), () =>
|
|
|
{
|
|
|
using var searcher = new ManagementObjectSearcher("select * from Win32_BaseBoard");
|
|
|
using var mos = searcher.Get();
|