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;

}

Welcome to my Blog

Hai all I am abhilash,
software student from India. I am onto programming from quite a time now. Then one day I suddenly thought about start blogging. So that I can share with you all whatever I know. I will try to give small code samples and all but I am not guaranteeing its correctness because still I am studying and I am careless and less worries about implementation. All the code given are simple and abstract . But I am sure even beginners can figure out what I ment, You can use the code at your own risk.

Thursday, February 19, 2009