Monday, June 29, 2009

One of the interesting subject when programming windows machine is Windows Registry. On this article, I will show you How you can perform a query from Windows Registry using Windows Management Instrumentation (WMI) Objects. WMI objects are exposed to .NET platform through System.Management Namespace. This namespace provides access to a rich set of management information and management events about the system, devices, and applications.


For today's example, we will retrieve the windows registry size.

First we must include the following namespaces on our Using directives.


Using System.Management;


Now to retrieve the size of the current machine's registry size, cut and paste the following code.

ManagementObjectSearcher mgmtObjects =
new ManagementObjectSearcher("Select * from Win32_Registry");

foreach (var mosItem in mgmtObjects.Get())
{
Console.WriteLine(string.Format("Current Size: {0}MB", mosItem["CurrentSize"]));
Console.WriteLine(string.Format("Maximum Size: {0}MB", mosItem["MaximumSize"]));
}


For more coding tips & tricks, subscribe now.

0 comments:

Post a Comment