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

本文关键字:程序 卸载 字符串 安装 何获取 获取 | 更新日期: 2023-09-27 18:06:49

我正试图从特定安装的应用程序中检索数据,例如安装文件夹,卸载字符串,版本号等。当我运行下面的代码时,我得到了Install Folder,但是它为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());
            }
        }
    }

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

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。你可以从中获得所需的详细信息。

这样如何:

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