如何获取已安装程序的卸载字符串

时间:2011-08-26 14:05:53

标签: c# registry installed-applications

我正在尝试从特定的已安装应用程序中检索数据,例如安装文件夹,卸载字符串,版本号等。当我运行以下代码时,我获得了安装文件夹,但它返回了四行问号UninstallString值。有什么想法吗?

    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(sbProductCode.ToString(), "UninstallString", sbInstallDir, ref installDirLen); 
                Console.Writeline("Uninstall String - {0}", sbInstallDir.ToString());

            }
        }
    }

2 个答案:

答案 0 :(得分:1)

UninstallString不是有效的属性。有关有效属性的列表,请参阅http://msdn.microsoft.com/en-us/library/aa370130(VS.85).aspx

如果打开Windows Installer头文件(“msi.h”)并搜索文本“UninstallString”,则无法找到它。此外,如果您在http://msdn.microsoft.com/en-us/library/aa370905(VS.85).aspx查看属性参考并在该页面中搜索“UninstallString”,您也将无法找到它。

我的建议是从注册表中读取属性。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/aa372105(VS.85).aspx。您可以从中获得所需的详细信息。

答案 1 :(得分:0)

这样的事情怎么样:

    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;
                }
            }
        }
    }