Saturday, February 21, 2009

Function to retrieve RAM SIZE in MB C#

This is the sample code to retrieve total ram size of system using WMI (Windows Management Instrumentation) well it’s not perfect but it works

Name spaces

using System;
using System.Management;
using System.Collections.Generic;
using System.Text;

public int getRAMSize() {
int total=0;
try
{
ConnectionOptions connection = new ConnectionOptions();
ManagementScope scope;
scope = new ManagementScope("root\\CIMV2", connection);
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
ManagementObjectSearcher searcher =new ManagementObjectSearcher(scope, query);
foreach (ManagementObject queryObj in searcher.Get(){

total = Convert.ToInt32(queryObj["TotalPhysicalMemory"].ToString());
}

total = total / 1024;
}

catch {} }

return total;

}

No comments:

Post a Comment