getting main memory (RAM) consumed in csharp using win32 API
 

 ulong installedMemory = 0;
            ChitrakshLibrary.API.MEMORYSTATUSEX memStatus = new ChitrakshLibrary.API.MEMORYSTATUSEX();
            if (ChitrakshLibrary.API.Win32Wrapper.GlobalMemoryStatusEx(memStatus))
            {
                textBox1.Text = "";
                
                installedMemory = memStatus.ullTotalPhys;
                textBox1.Text = "Total Memory=" + ChitrakshLibrary.CommonMethod.getSizeInMB_Or_GB(memStatus.ullTotalPhys);
                textBox1.Text = textBox1.Text + " Avialable Memory=" + ChitrakshLibrary.CommonMethod.getSizeInMB_Or_GB(memStatus.ullAvailPhys);
                textBox1.Text = textBox1.Text + " Used Memory=" + ChitrakshLibrary.CommonMethod.getSizeInMB_Or_GB(memStatus.ullTotalPhys - memStatus.ullAvailPhys);
               
                decimal c = (decimal)memStatus.ullTotalPhys - memStatus.ullAvailPhys;
                decimal d = memStatus.ullTotalPhys;
                decimal f = Math.Round((c / d) * 100, 0);
                progressBar1.Minimum = 0;
                progressBar1.Maximum = 100;
                progressBar1.Value = (int)(f);
                
                label2.Text = f.ToString() + "%";
            }

the API definitions are as under

 

public class MEMORYSTATUSEX
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public ulong ullAvailExtendedVirtual;
        public ulong ullAvailPageFile;
        public ulong ullAvailPhys;
        public ulong ullAvailVirtual;
        public ulong ullTotalPageFile;
        public ulong ullTotalPhys;
        public ulong ullTotalVirtual;

        public MEMORYSTATUSEX();
    }

and function is

public extern static bool GlobalMemoryStatusEx(MEMORYSTATUSEX lpBuffer);

share on whatapp
505 Views

Comments

Show All Amazon Product

Private Policy   Terms of Service