You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

150 lines
4.9 KiB

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IOTContainer.Common
{
public static class SystemUsage
{
#region 获取CPU使用率
#region AIP声明
[DllImport("IpHlpApi.dll")]
extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
[DllImport("User32")]
private extern static int GetWindow(int hWnd, int wCmd);
[DllImport("User32")]
private extern static int GetWindowLongA(int hWnd, int wIndx);
[DllImport("user32.dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
[DllImport("user32", CharSet = CharSet.Auto)]
private extern static int GetWindowTextLength(IntPtr hWnd);
#endregion
public static float? GetCpuUsedRate()
{
try
{
PerformanceCounter pcCpuLoad;
pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total")
{
MachineName = "."
};
pcCpuLoad.NextValue();
Thread.Sleep(1500);
float CpuLoad = pcCpuLoad.NextValue();
return CpuLoad;
}
catch
{
}
return 0;
}
#endregion
#region 获取内存使用率
#region 可用内存
/// <summary>
/// 获取可用内存
/// </summary>
internal static long? GetMemoryAvailable()
{
long availablebytes = 0;
var managementClassOs = new ManagementClass("Win32_OperatingSystem");
foreach (var managementBaseObject in managementClassOs.GetInstances())
if (managementBaseObject["FreePhysicalMemory"] != null)
availablebytes = 1024 * long.Parse(managementBaseObject["FreePhysicalMemory"].ToString());
return availablebytes / MbDiv;
}
#endregion
internal static double? GetMemoryUsed()
{
float? PhysicalMemory = GetPhysicalMemory();
float? MemoryAvailable = GetMemoryAvailable();
double? MemoryUsed = (double?)(PhysicalMemory - MemoryAvailable);
double currentMemoryUsed = (double)MemoryUsed;
return currentMemoryUsed;
}
private static long? GetPhysicalMemory()
{
//获得物理内存
var managementClass = new ManagementClass("Win32_ComputerSystem");
var managementObjectCollection = managementClass.GetInstances();
long PhysicalMemory;
foreach (var managementBaseObject in managementObjectCollection)
if (managementBaseObject["TotalPhysicalMemory"] != null)
{
return long.Parse(managementBaseObject["TotalPhysicalMemory"].ToString()) / MbDiv;
}
return null;
}
public static double? GetMemoryUsedRate()
{
float? PhysicalMemory = GetPhysicalMemory();
float? MemoryAvailable = GetMemoryAvailable();
double? MemoryUsedRate = (double?)(PhysicalMemory - MemoryAvailable) / PhysicalMemory;
return MemoryUsedRate.HasValue ? Convert.ToDouble(MemoryUsedRate * 100) : 0;
}
#endregion
#region 单位转换进制
private const int KbDiv = 1024;
private const int MbDiv = 1024 * 1024;
private const int GbDiv = 1024 * 1024 * 1024;
#endregion
#region 获取磁盘占用率
internal static double GetUsedDiskPercent()
{
float? usedSize = GetUsedDiskSize();
float? totalSize = GetTotalSize();
double? percent = (double?)usedSize / totalSize;
return percent.HasValue ? Convert.ToDouble(percent * 100) : 0;
}
internal static float? GetUsedDiskSize()
{
var currentDrive = GetCurrentDrive();
float UsedDiskSize = (long)currentDrive?.TotalSize - (long)currentDrive?.TotalFreeSpace;
return UsedDiskSize / MbDiv;
}
internal static float? GetTotalSize()
{
var currentDrive = GetCurrentDrive();
float TotalSize = (long)currentDrive?.TotalSize / MbDiv;
return TotalSize;
}
/// <summary>
/// 获取当前执行的盘符信息
/// </summary>
/// <returns></returns>
private static DriveInfo GetCurrentDrive()
{
string path = Application.StartupPath.ToString().Substring(0, 3);
return DriveInfo.GetDrives().FirstOrDefault<DriveInfo>(p => p.Name.Equals(path));
}
#endregion
}
}