Преглед на файлове

增加CPU、内存、硬盘信息检测

世龙 苏 преди 7 години
родител
ревизия
5daba31898
променени са 3 файла, в които са добавени 116 реда и са изтрити 1 реда
  1. 6 1
      CommonLibrary/CommonLibrary.csproj
  2. 99 0
      CommonLibrary/ComputerUtil.cs
  3. 11 0
      CommonLibrary/HardDiskInfo.cs

+ 6 - 1
CommonLibrary/CommonLibrary.csproj

@@ -30,6 +30,7 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Microsoft.VisualBasic" />
     <Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
       <HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
     </Reference>
@@ -62,6 +63,8 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="ComputerUtil.cs" />
+    <Compile Include="HardDiskInfo.cs" />
     <Compile Include="IpAddressAttribute.cs" />
     <Compile Include="MacAddress.cs" />
     <Compile Include="MacAddressAttribute.cs" />
@@ -69,7 +72,9 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="package.nuspec" />
-    <None Include="packages.config" />
+    <None Include="packages.config">
+      <SubType>Designer</SubType>
+    </None>
   </ItemGroup>
   <ItemGroup>
     <Analyzer Include="..\packages\StyleCop.Analyzers.1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />

+ 99 - 0
CommonLibrary/ComputerUtil.cs

@@ -0,0 +1,99 @@
+namespace CommonLibrary
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Diagnostics;
+    using System.IO;
+    using Microsoft.VisualBasic.Devices;
+
+    public class ComputerUtil : IDisposable
+    {
+        private readonly PerformanceCounter cpu;
+
+        private readonly ComputerInfo cinf;
+
+        public ComputerUtil()
+        {
+            this.cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
+            this.cinf = new ComputerInfo();
+        }
+
+        /// <summary>
+        /// cpu 使用情况
+        /// </summary>
+        /// <returns>cpu利用率</returns>
+        public double GetCpuPercent()
+        {
+            var percentage = this.cpu.NextValue();
+            return Math.Round(percentage, 2, MidpointRounding.AwayFromZero);
+        }
+
+        /// <summary>
+        /// 内存使用情况
+        /// </summary>
+        /// <returns>内存使用率</returns>
+        public double GetMemoryPercent()
+        {
+            var usedMem = this.cinf.TotalPhysicalMemory - this.cinf.AvailablePhysicalMemory; // 总内存减去可用内存
+            return Math.Round(
+                     (double)(usedMem / Convert.ToDecimal(this.cinf.TotalPhysicalMemory) * 100),
+                     2,
+                     MidpointRounding.AwayFromZero);
+        }
+
+        public HardDiskInfo GetHardDiskInfoByName(string diskName)
+        {
+            DriveInfo drive = new DriveInfo(diskName);
+            return new HardDiskInfo { FreeSpace = this.GetDriveData(drive.AvailableFreeSpace), TotalSpace = this.GetDriveData(drive.TotalSize), Name = drive.Name };
+        }
+
+        public string FreeSpaceHardDisk()
+        {
+            long userDisk = 0;
+            foreach (var d in DriveInfo.GetDrives())
+            {
+                if (d.IsReady)
+                {
+                    userDisk = userDisk + d.AvailableFreeSpace;
+                }
+            }
+
+            return this.GetDriveData(userDisk);
+        }
+
+        /// <summary>
+        /// 硬盘信息使用情况
+        /// </summary>
+        /// <returns>所有硬盘信息</returns>
+        public IEnumerable<HardDiskInfo> GetAllHardDiskInfo()
+        {
+            List<HardDiskInfo> infos = new List<HardDiskInfo>();
+            foreach (var d in DriveInfo.GetDrives())
+            {
+                if (d.IsReady)
+                {
+                    HardDiskInfo h = new HardDiskInfo
+                    {
+                        Name = d.Name,
+                        FreeSpace = this.GetDriveData(d.AvailableFreeSpace),
+                        TotalSpace = this.GetDriveData(d.TotalSize)
+                    };
+
+                    infos.Add(h);
+                }
+            }
+
+            return infos;
+        }
+
+        public void Dispose()
+        {
+            this.cpu.Dispose();
+        }
+
+        private string GetDriveData(long data)
+        {
+            return (data / Convert.ToDouble(1024) / Convert.ToDouble(1024) / Convert.ToDouble(1024)).ToString("0.00");
+        }
+    }
+}

+ 11 - 0
CommonLibrary/HardDiskInfo.cs

@@ -0,0 +1,11 @@
+namespace CommonLibrary
+{
+    public class HardDiskInfo
+    {
+        public string Name { get; set; }
+
+        public string FreeSpace { get; set; }
+
+        public string TotalSpace { get; set; }
+    }
+}