From 7159f607b06abe74bf861bbdd9f95c78ba4ee9c1 Mon Sep 17 00:00:00 2001 From: zuohuaijun Date: Sat, 26 Oct 2024 16:00:19 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8E1=E3=80=81=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=AE=A1=E7=AE=97=E6=9C=BA=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=85=BC=E5=AE=B9.net6=20=202.=E8=8E=B7=E5=8F=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=A1=A8(=E5=AE=9E=E4=BD=93)=E9=9B=86?= =?UTF-8?q?=E5=90=88=E9=87=87=E7=94=A8=E7=B2=BE=E7=A1=AE=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=20=203=E3=80=81=E5=8D=87=E7=BA=A7=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin.NET.Core/Admin.NET.Core.csproj | 2 +- .../Service/CodeGen/SysCodeGenService.cs | 2 +- .../Admin.NET.Core/Utils/ComputerUtil.cs | 31 ++++++++++++++----- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj b/Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj index a447ba0d..1b5acc8e 100644 --- a/Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj +++ b/Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj @@ -13,7 +13,7 @@ - + diff --git a/Admin.NET/Admin.NET.Core/Service/CodeGen/SysCodeGenService.cs b/Admin.NET/Admin.NET.Core/Service/CodeGen/SysCodeGenService.cs index d5ff9053..7992167f 100644 --- a/Admin.NET/Admin.NET.Core/Service/CodeGen/SysCodeGenService.cs +++ b/Admin.NET/Admin.NET.Core/Service/CodeGen/SysCodeGenService.cs @@ -190,7 +190,7 @@ public class SysCodeGenService : IDynamicApiController, ITransient if (bracketIndex != -1) dbTableName = dbTableName.Substring(0, bracketIndex); - var table = dbTableInfos.FirstOrDefault(u => u.Name.ToLower().StartsWith((config != null && config.DbSettings.EnableUnderLine ? UtilMethods.ToUnderLine(dbTableName) : dbTableName).ToLower())); + var table = dbTableInfos.FirstOrDefault(u => u.Name.ToLower().Equals((config != null && config.DbSettings.EnableUnderLine ? UtilMethods.ToUnderLine(dbTableName) : dbTableName).ToLower())); if (table == null) continue; tableOutputList.Add(new TableOutput { diff --git a/Admin.NET/Admin.NET.Core/Utils/ComputerUtil.cs b/Admin.NET/Admin.NET.Core/Utils/ComputerUtil.cs index c6547cb4..09c50657 100644 --- a/Admin.NET/Admin.NET.Core/Utils/ComputerUtil.cs +++ b/Admin.NET/Admin.NET.Core/Utils/ComputerUtil.cs @@ -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; @@ -205,10 +208,10 @@ public static class ComputerUtil string runTime = string.Empty; if (IsMacOS()) { - //macOS 获取系统启动时间: - //sysctl -n kern.boottime | awk '{print $4}' | tr -d ',' - //返回:1705379131 - //使用date格式化即可 + // macOS 获取系统启动时间: + // sysctl -n kern.boottime | awk '{print $4}' | tr -d ',' + // 返回:1705379131 + // 使用date格式化即可 string output = ShellUtil.Bash("date -r $(sysctl -n kern.boottime | awk '{print $4}' | tr -d ',') +\"%Y-%m-%d %H:%M:%S\"").Trim(); runTime = DateTimeUtil.FormatTime((DateTime.Now - output.ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong()); } @@ -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 /// 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; @@ -447,6 +456,7 @@ public class ShellUtil /// 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(); @@ -457,6 +467,9 @@ public class ShellUtil output.AppendLine(outputItem.BaseObject.ToString()); } return output.ToString(); +#else + return ""; +#endif } } @@ -518,6 +531,7 @@ public class ShellHelper /// 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(); @@ -528,5 +542,8 @@ public class ShellHelper output.AppendLine(outputItem.BaseObject.ToString()); } return output.ToString(); +#else + return ""; +#endif } } \ No newline at end of file