如何獲取系統可用磁盤和可用內存?html
這個有自帶的 DriveInfo 能夠用。
DriveInfo 類 (System.IO) | Microsoft Docsapi
參考:C#得到系統內存佔用狀況 | 柊ui
偷懶能夠直接看:code
//定義內存的信息結構 [StructLayout(LayoutKind.Sequential)] public 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 } class MemoryHelper { [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GlobalMemoryStatusEx(ref MEMORY_INFO mi); /// <summary> /// 得到當前內存使用狀況 /// </summary> /// <returns></returns> private static MEMORY_INFO GetMemoryStatus() { MEMORY_INFO mi = new MEMORY_INFO(); mi.dwLength = (uint) System.Runtime.InteropServices.Marshal.SizeOf(mi); GlobalMemoryStatusEx(ref mi); return mi; } /// <summary> /// 得到當前可用物理內存大小 /// </summary> /// <returns>當前可用物理內存(B)</returns> private static ulong GetAvailPhys() { MEMORY_INFO mi = GetMemoryStatus(); return mi.ullAvailPhys; } public static ulong GetAvailablePhysMemorySize() { try { return GetAvailPhys(); } catch (Exception) { return 0; } } }