refactor(Admin.NET.Core): 适配 .NET 8.0 及以上版本的 CPU、运行时间和内存指标获取
- 更新了 ComputerUtil 和 MemoryMetricsClient 类中的方法,以支持 .NET 8.0 及以上版本 - 保留了原有 .NET 版本的兼容性代码 - 优化了代码结构,提高了可读性和可维护性
This commit is contained in:
parent
729c0c60f8
commit
22fd588830
@ -189,8 +189,11 @@ public static class ComputerUtil
|
||||
}
|
||||
else
|
||||
{
|
||||
#if NET8_0 || NET9_0
|
||||
string output = ShellUtil.Cmd("wmic", "cpu get LoadPercentage");
|
||||
// string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_Processor | Measure-Command {Start-Sleep -Seconds 1}");
|
||||
#else
|
||||
string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_Processor | Measure-Command {Start-Sleep -Seconds 1}");
|
||||
#endif
|
||||
cpuRates.AddRange(output.Replace("LoadPercentage", string.Empty).Trim().Split("\r\r\n"));
|
||||
}
|
||||
return cpuRates;
|
||||
@ -219,8 +222,11 @@ public static class ComputerUtil
|
||||
}
|
||||
else
|
||||
{
|
||||
#if NET8_0 || NET9_0
|
||||
string output = ShellUtil.Cmd("wmic", "OS get LastBootUpTime/Value");
|
||||
// string output = ShellUtil.PowerShell("(Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime");
|
||||
#else
|
||||
string output = ShellUtil.PowerShell("(Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime");
|
||||
#endif
|
||||
string[] outputArr = output.Split('=', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
if (outputArr.Length == 2)
|
||||
runTime = DateTimeUtil.FormatTime((DateTime.Now - outputArr[1].Split('.')[0].ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
||||
@ -331,8 +337,11 @@ public class MemoryMetricsClient
|
||||
/// <returns></returns>
|
||||
public static MemoryMetrics GetWindowsMetrics()
|
||||
{
|
||||
#if NET8_0 || NET9_0
|
||||
string output = ShellUtil.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value");
|
||||
// string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_OperatingSystem | Select FreePhysicalMemory, TotalVisibleMemorySize");
|
||||
#else
|
||||
string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_OperatingSystem | Select FreePhysicalMemory, TotalVisibleMemorySize");
|
||||
#endif
|
||||
var metrics = new MemoryMetrics();
|
||||
var lines = output.Trim().Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
if (lines.Length <= 0) return metrics;
|
||||
@ -448,7 +457,6 @@ public class ShellUtil
|
||||
public static string PowerShell(string script)
|
||||
{
|
||||
#if NET8_0 || NET9_0
|
||||
// net 8.0 可用
|
||||
using var PowerShellInstance = System.Management.Automation.PowerShell.Create();
|
||||
PowerShellInstance.AddScript(script);
|
||||
var PSOutput = PowerShellInstance.Invoke();
|
||||
@ -459,6 +467,9 @@ public class ShellUtil
|
||||
output.AppendLine(outputItem.BaseObject.ToString());
|
||||
}
|
||||
return output.ToString();
|
||||
#else
|
||||
return "";
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@ -520,6 +531,7 @@ public class ShellHelper
|
||||
/// <returns></returns>
|
||||
public static string PowerShell(string script)
|
||||
{
|
||||
#if NET8_0 || NET9_0
|
||||
using var PowerShellInstance = System.Management.Automation.PowerShell.Create();
|
||||
PowerShellInstance.AddScript(script);
|
||||
var PSOutput = PowerShellInstance.Invoke();
|
||||
@ -530,5 +542,8 @@ public class ShellHelper
|
||||
output.AppendLine(outputItem.BaseObject.ToString());
|
||||
}
|
||||
return output.ToString();
|
||||
#else
|
||||
return "";
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user