LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 获取Windows系统信息以及CPU、内存和磁盘使用情况

admin
2023年2月27日 10:36 本文热度 2172

    一个简单的小工具,主要用来获取当期的电脑信息以及内存的使用情况。本来也没想着写这个玩意,还不如随便下个相关的软件好使,但是前一段时间为了追查系统卡顿的问题,所以就加上了一段统计内存的代码,用来看看是否由这个引起的。

既然统计了内存相关的数据,干脆一不做二不休,又加了下CPU以及硬盘的使用情况统计。

统计的准确不准确我也不好说,因为我也不太能确定,暂且就做个参考值吧。或许使用很多大佬封装过的第三方DLL会准确很多。

下面工具中还展示了部分关于电脑的基本信息数据,这个在之前的时候应该是有发过,我只是单纯的拿了过来(功能实现的多看着洋气)。

主要还是使用windows WMI,虽然下列关于内存情况的统计没用(但其实也可以统计到),因为我感觉这个方式可能稍微准确点。。。

实现功能:

    • 获取系统内存、CPU的使用情况

开发环境:

开发工具:Visual Studio 2013

.NET Framework版本:4.5

实现代码:

 public class SystemUtil    {        #region 内存
#region 获得内存信息API [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GlobalMemoryStatusEx(ref MEMORY_INFO mi); //定义内存的信息结构 [StructLayout(LayoutKind.Sequential)] private struct MEMORY_INFO { public uint DWLength; //当前结构体大小 public uint DWMemoryLoad; //当前内存使用率 public ulong ullTotalPhys; //总计物理内存大小 public ulong ullAvailPhys; //可用物理内存大小 public ulong ullTotalPagefile; //总计交换文件大小 public ulong ullAvailPagefile; //总计交换文件大小 public ulong ullTotalVirtual; //总计虚拟内存大小 public ulong ullAvailVirtual; //可用虚拟内存大小 public ulong ullAvailExtendedVirtual; //保留 这个值始终为0 }
private static MEMORY_INFO GetMemoryInfo() { MEMORY_INFO MemoryInfo = new MEMORY_INFO(); MemoryInfo.DWLength = (uint)System.Runtime.InteropServices.Marshal.SizeOf(MemoryInfo); GlobalMemoryStatusEx(ref MemoryInfo); return MemoryInfo; } #endregion
/// <summary> /// 获取系统内存 /// </summary> /// <returns></returns> public static string GetSysMemory() { MEMORY_INFO MemoryInfo = GetMemoryInfo(); return ConvertBytes((long)MemoryInfo.ullTotalPhys); }
/// <summary> /// 获取剩余内存 /// </summary> /// <returns></returns> public static string GetUnUsedMemory() { MEMORY_INFO MemoryInfo = GetMemoryInfo(); return ConvertBytes((long)MemoryInfo.ullAvailPhys); }

/// <summary> /// 获取已使用内存 /// </summary> /// <returns></returns> public static string GetUsedMemory() { MEMORY_INFO MemoryInfo = GetMemoryInfo();
return ConvertBytes((long)(MemoryInfo.ullTotalPhys - MemoryInfo.ullAvailPhys)); }

/// <summary> /// 获取应用使用内存 /// </summary> /// <param name="appName"></param> /// <returns></returns> public static string GetAppMemory(string appName) { Process CurrentProcess = Process.GetProcessesByName(appName)[0]; PerformanceCounter Perform = new PerformanceCounter("Process", "Working Set - Private", CurrentProcess.ProcessName); return ConvertBytes((long)Perform.NextValue()); }
/// <summary> /// 获取当前应用使用内存 /// </summary> /// <returns></returns> public static string GetCurrentAppMemory() { long memory = Process.GetCurrentProcess().PrivateMemorySize64; return ConvertBytes(memory); }

#endregion
#region CPU /// <summary> /// 获取CPU使用率 /// </summary> /// <returns></returns> public static string GetUsedCPU() { ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfOS_Processor"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>();
foreach (ManagementObject mo in moc) { if (mo["Name"].ToString() == "_Total") { list.Add(mo["PercentProcessorTime"].ToString()); } } return list.Sum(s => int.Parse(s)) + "%"; } #endregion
#region 硬盘 public static string GetUsedDisk() { ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfDisk_PhysicalDisk"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>();
foreach (ManagementObject mo in moc) { if (mo["Name"].ToString() == "_Total") { list.Add(mo["PercentDiskTime"].ToString()); } } return list.Sum(s => int.Parse(s)) + "%"; }
#endregion
#region 电脑信息 /// <summary> /// 获取CPU序列号 /// </summary> /// <returns></returns> public static string GetCpuID() { ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo.Properties["ProcessorId"].Value.ToString()); } return string.Join("|", list);
//若需要获取所有属性,可迭代ManagementObject.Properties //foreach (PropertyData pd in mo.Properties) //{ // Console.WriteLine(pd.Name + "----" + pd.Value); //} }
/// <summary> /// 获取Mac地址 /// </summary> /// <returns></returns> public static string GetMac() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { list.Add(mo["MacAddress"].ToString()); } } return string.Join("|", list); }
/// <summary> /// 获取IP地址 /// </summary> /// <returns></returns> public static string GetIp() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { list.Add(((Array)mo.Properties["IpAddress"].Value).GetValue(0).ToString()); } } return string.Join("|", list); }
/// <summary> /// 获取硬盘序列号 /// </summary> /// <returns></returns> public static string GetDiskID() { ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo.Properties["Model"].Value.ToString()); } return string.Join("|", list); }
/// <summary> /// 获取系统名称 /// </summary> /// <returns></returns> public static string GetSystemName() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["Name"].ToString()); } return string.Join("|", list); // return System.Environment.GetEnvironmentVariable("ComputerName") }
/// <summary> /// 获取当前登录用户 /// </summary> /// <returns></returns> public static string GetLoginUser() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["UserName"].ToString()); } return string.Join("|", list); }
/// <summary> /// 获取电脑类型 /// </summary> /// <returns></returns> public static string GetPcType() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["SystemType"].ToString()); } return string.Join("|", list); }
/// <summary> /// 获取系统内存 /// </summary> /// <returns></returns> public static string GetSysMemory2() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["TotalPhysicalMemory"].ToString()); } return string.Join("|", list); }
/// <summary> /// 获取剩余内存 /// </summary> /// <returns></returns> public static string GetUnUsedMemory2() { ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory"); ManagementObjectCollection moc = mc.GetInstances(); long memory = 0; foreach (ManagementObject mo in moc) { memory += long.Parse(mo.Properties["AvailableMBytes"].Value.ToString()); } return ConvertBytes(memory); }
/// <summary> /// 获取电脑品牌 /// </summary> /// <returns></returns> public static string GetPcBrand() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["Manufacturer"].ToString()); } return string.Join("|", list); }
/// <summary> /// 获取电脑型号 /// </summary> /// <returns></returns> public static string GetPcModel() { ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); List<string> list = new List<string>(); foreach (ManagementObject mo in moc) { list.Add(mo["SystemFamily"].ToString()); } return string.Join("|", list); } #endregion
public static string ConvertBytes(long len) { double dlen = len; string[] sizes = { "B", "KB", "MB", "GB", "TB" }; int order = 0; while (dlen >= 1024 && order + 1 < sizes.Length) { order++; dlen = dlen / 1024; } return String.Format("{0:0.##} {1}", dlen, sizes[order]); } }

实现效果:



该文章在 2023/2/27 10:36:58 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved