懒得勤快 5 years ago
parent
commit
b22b5d26ac
1 changed files with 28 additions and 32 deletions
  1. 28 32
      Masuit.Tools.Abstractions/Hardware/SystemInfo.cs

+ 28 - 32
Masuit.Tools.Abstractions/Hardware/SystemInfo.cs

@@ -271,7 +271,7 @@ namespace Masuit.Tools.Hardware
         /// 获取CPU温度
         /// </summary>
         /// <returns>CPU温度</returns>
-        public static double GetCPUTemperature()
+        public static float GetCPUTemperature()
         {
             try
             {
@@ -284,8 +284,8 @@ namespace Masuit.Tools.Hardware
                 }
 
                 //这就是CPU的温度了
-                double temp = (double.Parse(str) - 2732) / 10;
-                return Math.Round(temp, 2);
+                float temp = (float.Parse(str) - 2732) / 10;
+                return (float)Math.Round(temp, 2);
             }
             catch (Exception)
             {
@@ -303,7 +303,7 @@ namespace Masuit.Tools.Hardware
         /// <returns></returns>
         public static string GetProcessorData()
         {
-            double d = GetCounterValue(CpuCounter, "Processor", "% Processor Time", "_Total");
+            float d = GetCounterValue(CpuCounter, "Processor", "% Processor Time", "_Total");
             return CompactFormat ? (int)d + "%" : d.ToString("F") + "%";
         }
 
@@ -317,12 +317,10 @@ namespace Masuit.Tools.Hardware
         /// <returns></returns>
         public static string GetMemoryVData()
         {
-            double d = GetCounterValue(MemoryCounter, "Memory", "% Committed Bytes In Use", null);
+            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) + ") ";
         }
@@ -331,7 +329,7 @@ namespace Masuit.Tools.Hardware
         /// 获取虚拟内存使用率
         /// </summary>
         /// <returns></returns>
-        public static double GetUsageVirtualMemory()
+        public static float GetUsageVirtualMemory()
         {
             return GetCounterValue(MemoryCounter, "Memory", "% Committed Bytes In Use", null);
         }
@@ -340,7 +338,7 @@ namespace Masuit.Tools.Hardware
         /// 获取虚拟内存已用大小
         /// </summary>
         /// <returns></returns>
-        public static double GetUsedVirtualMemory()
+        public static float GetUsedVirtualMemory()
         {
             return GetCounterValue(MemoryCounter, "Memory", "Committed Bytes", null);
         }
@@ -349,7 +347,7 @@ namespace Masuit.Tools.Hardware
         /// 获取虚拟内存总大小
         /// </summary>
         /// <returns></returns>
-        public static double GetTotalVirtualMemory()
+        public static float GetTotalVirtualMemory()
         {
             return GetCounterValue(MemoryCounter, "Memory", "Commit Limit", null);
         }
@@ -365,11 +363,9 @@ namespace Masuit.Tools.Hardware
         public static string GetMemoryPData()
         {
             string s = QueryComputerSystem("totalphysicalmemory");
-            double totalphysicalmemory = Convert.ToDouble(s);
-
-            double d = GetCounterValue(MemoryCounter, "Memory", "Available Bytes", null);
+            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;
@@ -380,17 +376,17 @@ namespace Masuit.Tools.Hardware
         /// 获取物理内存总数,单位B
         /// </summary>
         /// <returns></returns>
-        public static double GetTotalPhysicalMemory()
+        public static float GetTotalPhysicalMemory()
         {
             string s = QueryComputerSystem("totalphysicalmemory");
-            return s.ToDouble();
+            return s.TryConvertTo<float>();
         }
 
         /// <summary>
         /// 获取空闲的物理内存数,单位B
         /// </summary>
         /// <returns></returns>
-        public static double GetFreePhysicalMemory()
+        public static float GetFreePhysicalMemory()
         {
             return GetCounterValue(MemoryCounter, "Memory", "Available Bytes", null);
         }
@@ -399,7 +395,7 @@ namespace Masuit.Tools.Hardware
         /// 获取已经使用了的物理内存数,单位B
         /// </summary>
         /// <returns></returns>
-        public static double GetUsedPhysicalMemory()
+        public static float GetUsedPhysicalMemory()
         {
             return GetTotalPhysicalMemory() - GetFreePhysicalMemory();
         }
@@ -413,7 +409,7 @@ namespace Masuit.Tools.Hardware
         /// </summary>
         /// <param name="dd">读或写</param>
         /// <returns></returns>
-        public static double 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;
+        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
 
@@ -424,18 +420,18 @@ namespace Masuit.Tools.Hardware
         /// </summary>
         /// <param name="nd">上传或下载</param>
         /// <returns></returns>
-        public static double GetNetData(NetData nd)
+        public static float GetNetData(NetData nd)
         {
             if (InstanceNames.Length == 0)
             {
                 return 0;
             }
 
-            double d = 0;
+            float d = 0;
             for (int i = 0; i < InstanceNames.Length; i++)
             {
-                double receied = GetCounterValue(NetRecvCounters[i], "Network Interface", "Bytes Received/sec", InstanceNames[i]);
-                double send = GetCounterValue(NetSentCounters[i], "Network Interface", "Bytes Sent/sec", InstanceNames[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:
@@ -597,7 +593,7 @@ namespace Masuit.Tools.Hardware
                 {
                     if (null != mo["DeviceID"] && null != mo["FreeSpace"])
                     {
-                        dic.Add(mo["DeviceID"].ToString(), FormatBytes(double.Parse(mo["FreeSpace"].ToString())));
+                        dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["FreeSpace"].ToString())));
                     }
                 }
 
@@ -626,7 +622,7 @@ namespace Masuit.Tools.Hardware
                 {
                     if (null != mo["DeviceID"] && null != mo["Size"])
                     {
-                        dic.Add(mo["DeviceID"].ToString(), FormatBytes(double.Parse(mo["Size"].ToString())));
+                        dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["Size"].ToString())));
                     }
                 }
 
@@ -654,7 +650,7 @@ namespace Masuit.Tools.Hardware
                     if (null != mo["DeviceID"] && null != mo["Size"])
                     {
                         var free = mo["FreeSpace"];
-                        dic.Add(mo["DeviceID"].ToString(), FormatBytes(double.Parse(mo["Size"].ToString()) - free.ToString().ToDouble()));
+                        dic.Add(mo["DeviceID"].ToString(), FormatBytes(float.Parse(mo["Size"].ToString()) - free.ToString().ToDouble()));
                     }
                 }
 
@@ -673,11 +669,11 @@ namespace Masuit.Tools.Hardware
         /// 获取磁盘使用率
         /// </summary>
         /// <returns></returns>
-        public static Dictionary<string, double> DiskUsage()
+        public static Dictionary<string, float> DiskUsage()
         {
             try
             {
-                var dic = new Dictionary<string, double>();
+                var dic = new Dictionary<string, float>();
                 using var mos = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
                 foreach (var mo in mos.Get())
                 {
@@ -686,9 +682,9 @@ namespace Masuit.Tools.Hardware
                     {
                         var free = mo["FreeSpace"];
                         var total = mo["Size"];
-                        if (null != total && total.ToString().ToDouble() > 0)
+                        if (null != total && total.ToString().TryConvertTo<float>() > 0)
                         {
-                            dic.Add(device.ToString(), 1 - free.ToString().ToDouble() / total.ToString().ToDouble());
+                            dic.Add(device.ToString(), 1 - free.ToString().TryConvertTo<float>() / total.ToString().TryConvertTo<float>());
                         }
                     }
                 }
@@ -697,7 +693,7 @@ namespace Masuit.Tools.Hardware
             }
             catch (Exception)
             {
-                return new Dictionary<string, double>()
+                return new Dictionary<string, float>()
                 {
                     { "未能获取到当前计算机的磁盘信息,可能是当前程序无管理员权限,如果是web应用程序,请将应用程序池的高级设置中的进程模型下的标识设置为:LocalSystem;如果是普通桌面应用程序,请提升管理员权限后再操作。", 0 }
                 };
@@ -706,7 +702,7 @@ namespace Masuit.Tools.Hardware
 
         #endregion
 
-        private static double GetCounterValue(PerformanceCounter pc, string categoryName, string counterName, string instanceName)
+        private static float GetCounterValue(PerformanceCounter pc, string categoryName, string counterName, string instanceName)
         {
             pc.CategoryName = categoryName;
             pc.CounterName = counterName;