Menu

Thursday, January 24, 2008

Windows Management Instrumentation! Its coool! :-)

hi guys,

I just like to give you a brief idea about WMI interface. Actually it’s a kind of efficient way to access OS resources using programming modules. In another words it’s a bridge between your Windows OS and programming interface. Using WMI interface you can programmatically change computer settings as well as do many operations which your administrator can do on your computer.

Actually it was very helpful for me once my PC infected with a damn virus and all administrative settings were disabled coz of it. So what I have done was I used WMI interface to access to OS resources including registry entries to change some settings. As well as if you are a developer it’s wide area you need to study.

Basically WMI used WMI Query Language (WQL) to manipulate system resources. It’s just like SQL queries; if you are familiar with SQL it’s easy to make adjustment to WQL. Actually it’s hard to give a clear definition on this, but try to think like WMI as a repository of properties and methods related to the system environment that you canaccess like a database.

You need a few different objects to perform WMI queries in .Net. They include the following:

(all within System.Management namespace)
ConnectionOptions
ManagementScope
ObjectQuery
ManagementObjectSearcher
ManagementObjectCollection
ManagementObject

Here is a sample program that I used to search folders

public static void FindFoldersByName(string strName)
{

try
{
// Execute WMI Query and wait for result
WqlObjectQuery wqlObjectQuery = new WqlObjectQuery
("SELECT Name FROM CIM_Directory WHERE FileName = '" + strName + "'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wqlObjectQuery);
ManagementObjectCollection collFolders = searcher.Get();

// Display each folder path...
foreach (ManagementObject folder in collFolders)
{
string strCurrentPath = folder.Properties["Name"].Value.ToString();
MessageBox.Show(strCurrentPath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

More on this:-
http://msdn2.microsoft.com/en-us/library/aa394582%28VS.85%29.aspx
http://msdn2.microsoft.com/en-us/library/aa510211.aspx
http://www.codeguru.com/csharp/csharp/cs_network/wmi/article.php/c6035/#WMI