How do I go about getting the Uninstall String for an installed program
I am trying to retrieve data from a particular installed application, such as the Installation Folder, Uninstall String, Version number, etc. When I run the following code, I get the Install Folder but it returns four rows of question marks for the UninstallString value. Any ideas?
public static void FindInstalled(string AppName)
{
StringBuilder sbProductCode = new StringBuilder(39);
int iIdx = 0;
while (
0 == MsiEnumProducts(iIdx++, sbProductCode))
{
Int32 productNameLen = 512;
StringBuilder sbProductName = new StringBuilder(productNameLen);
MsiGetProductInfo(sbProductCode.ToString(), "ProductName", sbProductName, ref productNameLen);
if (sbProductName.ToString().Contains(AppName))
{
Int32 installDirLen = 1024;
StringBuilder sbInstallDir = new StringBuilder(installDirLen);
MsiGetProductInfo(sbProductCode.ToString(), "InstallLocation", sbInstallDir, ref installDirLen);
Console.Writeline("Install Directory - {0}",sbInstallDir.ToString());
MsiGetProductInfo(sbProdu开发者_如何学PythonctCode.ToString(), "UninstallString", sbInstallDir, ref installDirLen);
Console.Writeline("Uninstall String - {0}", sbInstallDir.ToString());
}
}
}
UninstallString isn't a valid property. See http://msdn.microsoft.com/en-us/library/aa370130(VS.85).aspx for a list of valid properties.
If you open the Windows Installer header file ("msi.h") and search for the text "UninstallString", you won't find it. Also if you look in the property reference at http://msdn.microsoft.com/en-us/library/aa370905(VS.85).aspx and search that page for "UninstallString", you won't find it either.
My advice would be to read the properties out of the registry instead. See http://msdn.microsoft.com/en-us/library/aa372105(VS.85).aspx for details. You can get the details you need from that.
How about something like this:
public static void FindInstalled(AppName)
{
RegistryKey myRegKey = Registry.LocalMachine;
myRegKey = myRegKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
String[] subkeyNames = myRegKey.GetSubKeyNames();
foreach (String s in subkeyNames)
{
RegistryKey UninstallKey = Registry.LocalMachine;
UninstallKey = UninstallKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + s);
Object oValue = UninstallKey.GetValue("DisplayName");
if (oValue != null)
{
if (oValue.ToString() == AppName)
{
oValue = UninstallKey.GetValue("UninstallString");
Console.Writeline("Uninstall URL - {0}", oValue.ToString());
break;
}
}
}
}
精彩评论