Thursday, December 4, 2008

Getting the List of Installed softwares from the local system

Here is the sample code to get the list of softwares installed in the system.


const uint HKEY_LOCAL_MACHINE = unchecked((uint)0x80000002);
ManagementClass wmiRegistry = new ManagementClass("root/default",
"StdRegProv", null);
//Enumerate subkeys
string keyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
object[] methodArgs = new object[] { HKEY_LOCAL_MACHINE, keyPath, null };
uint returnValue = (uint)wmiRegistry.InvokeMethod("EnumKey", methodArgs);
MessageBox.Show("Executing EnumKey() returns: " + returnValue);
if (null != methodArgs[2])
{
string[] subKeys = methodArgs[2] as String[];
if (subKeys == null) return;
ManagementBaseObject inParam =
wmiRegistry.GetMethodParameters("GetStringValue");
inParam["hDefKey"] = HKEY_LOCAL_MACHINE;
string keyName = "";

foreach (string subKey in subKeys)
{
//Display application name
keyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" +
subKey;
keyName = "DisplayName";
inParam["sSubKeyName"] = keyPath;
inParam["sValueName"] = keyName;
ManagementBaseObject outParam =
wmiRegistry.InvokeMethod("GetStringValue", inParam, null);

if ((uint)outParam["ReturnValue"] == 0)
{
listBox1.Items.Add(outParam["sValue"]);
}
}
}

No comments: