×

# C# window api 获取内存使用率

hqy hqy 发表于2026-04-08 16:57:20 浏览8 评论0

抢沙发发表评论

# C# window api 获取内存使用率

代码

C#
public static class WindowsMemory{    public static double GetMemoryUsage()    {        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))        {            MEMORYSTATUSEX memoryStatus = new MEMORYSTATUSEX();            memoryStatus.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));            if (GlobalMemoryStatusEx(ref memoryStatus) == false)            {                return 0;            }            return Math.Round((float)(memoryStatus.ullTotalPhys - memoryStatus.ullAvailPhys) / memoryStatus.ullTotalPhys * 100f, 2);        }        return 0;    }    [DllImport("kernel32.dll")]    [return: MarshalAs(UnmanagedType.Bool)]    public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);    [StructLayout(LayoutKind.Sequential)]    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;    }}


打赏

本文链接:https://kinber.cn/post/6412.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客