编辑
2025-09-20
C#
00

目录

获取CPU信息
获取内存信息
获取磁盘信息

在C#中,我们可以使用Windows API来获取系统信息,例如获取CPU信息、内存信息、磁盘信息等。下面我们将演示如何使用C#调用系统Windows API来读取系统信息。

获取CPU信息

  • processorArchitecture: 表示处理器架构,例如 x86、x64 等
  • pageSize: 表示页面大小,通常是内存页面的大小,以字节为单位
  • minimumApplicationAddress: 表示应用程序可以使用的最小内存地址
  • maximumApplicationAddress: 表示应用程序可以使用的最大内存地址
  • activeProcessorMask: 表示活动处理器的位掩码
  • numberOfProcessors: 表示系统中的处理器数量
  • processorType: 表示处理器类型
  • allocationGranularity: 表示内存分配的粒度
  • processorLevel: 表示处理器级别
  • processorRevision: 表示处理器修订版本
C#
using System; using System.Runtime.InteropServices; class Program { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct SYSTEM_INFO { public ushort processorArchitecture; public uint pageSize; public IntPtr minimumApplicationAddress; public IntPtr maximumApplicationAddress; public IntPtr activeProcessorMask; public uint numberOfProcessors; public uint processorType; public uint allocationGranularity; public ushort processorLevel; public ushort processorRevision; } [DllImport("kernel32.dll")] public static extern void GetSystemInfo(out SYSTEM_INFO si); static void Main() { SYSTEM_INFO si; GetSystemInfo(out si); Console.WriteLine("Number of Processors: " + si.numberOfProcessors); Console.WriteLine("Processor Type: " + si.processorType); } }

image.png

获取内存信息

  • dwLength: 结构体的长度,需要在调用 GlobalMemoryStatusEx 函数之前设置为结构体的大小
  • dwMemoryLoad: 表示当前系统内存的使用率,以百分比表示
  • ullTotalPhys: 表示系统的总物理内存大小,以字节为单位
  • ullAvailPhys: 表示系统当前可用的物理内存大小,以字节为单位
  • ullTotalPageFile: 表示系统的总页面文件大小,以字节为单位
  • ullAvailPageFile: 表示系统当前可用的页面文件大小,以字节为单位
  • ullTotalVirtual: 表示系统的总虚拟内存大小,以字节为单位
  • ullAvailVirtual: 表示系统当前可用的虚拟内存大小,以字节为单位
  • ullAvailExtendedVirtual: 表示系统当前进程可以使用的扩展虚拟内存大小,以字节为单位
C#
class Program { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct MEMORYSTATUSEX { 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; } [DllImport("kernel32.dll")] public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); static void Main() { MEMORYSTATUSEX memStatus = new MEMORYSTATUSEX(); memStatus.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); if (GlobalMemoryStatusEx(ref memStatus)) { Console.WriteLine("Total Physical Memory: " + memStatus.ullTotalPhys); Console.WriteLine("Available Physical Memory: " + memStatus.ullAvailPhys); Console.WriteLine("Total Virtual Memory: " + memStatus.ullTotalVirtual); Console.WriteLine("Available Virtual Memory: " + memStatus.ullAvailVirtual); } else { Console.WriteLine("Failed to retrieve memory status."); } } }

image.png

获取磁盘信息

C#
class Program { // 定义用于获取磁盘信息的结构体 [StructLayout(LayoutKind.Sequential)] public struct DiskInfo { public uint BytesPerSector; public uint SectorsPerCluster; public uint NumberOfFreeClusters; public uint TotalNumberOfClusters; } // 声明用于获取磁盘信息的Windows API函数 [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GetDiskFreeSpace(string lpRootPathName, out uint lpSectorsPerCluster, out uint lpBytesPerSector, out uint lpNumberOfFreeClusters, out uint lpTotalNumberOfClusters); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes); static void Main() { // 获取磁盘信息 string rootPath = "C:\\"; // 选择要获取信息的磁盘根目录 uint sectorsPerCluster, bytesPerSector, numberOfFreeClusters, totalNumberOfClusters; bool result = GetDiskFreeSpace(rootPath, out sectorsPerCluster, out bytesPerSector, out numberOfFreeClusters, out totalNumberOfClusters); if (result) { // 将获取到的磁盘信息存储到结构体中 DiskInfo diskInfo = new DiskInfo { BytesPerSector = bytesPerSector, SectorsPerCluster = sectorsPerCluster, NumberOfFreeClusters = numberOfFreeClusters, TotalNumberOfClusters = totalNumberOfClusters }; // 输出获取到的磁盘信息 Console.WriteLine("磁盘信息:"); Console.WriteLine("每个簇的扇区数: " + diskInfo.SectorsPerCluster); Console.WriteLine("每个扇区的字节数: " + diskInfo.BytesPerSector); Console.WriteLine("可用的簇数: " + diskInfo.NumberOfFreeClusters); Console.WriteLine("簇的总数: " + diskInfo.TotalNumberOfClusters); } else { Console.WriteLine("获取磁盘信息失败!"); } // 获取磁盘大小信息 ulong freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes; bool sizeResult = GetDiskFreeSpaceEx(rootPath, out freeBytesAvailable, out totalNumberOfBytes, out totalNumberOfFreeBytes); if (sizeResult) { // 输出获取到的磁盘大小信息 Console.WriteLine("磁盘大小信息:"); Console.WriteLine("可用空间: " + freeBytesAvailable + " bytes"); Console.WriteLine("总空间: " + totalNumberOfBytes + " bytes"); Console.WriteLine("可用空间总数: " + totalNumberOfFreeBytes + " bytes"); } else { Console.WriteLine("获取磁盘大小信息失败!"); } } }

image.png

以上就是使用C#调用系统Windows API读取系统信息的示例代码,通过这些示例代码,我们可以获取到系统的CPU信息、内存信息和磁盘信息。希望对你有所帮助!

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!