本文介绍了获取已安装的Msi的产品代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#程序,必须在其中获取已安装的msi的产品代码.我只有msi名称作为输入.可以通过编程方式完成吗?

I have a C# program where I have to get the product code of an installed msi. I have only the msi name as the input. Can this be done programmatically?

推荐答案

这是我用来获取任何MSI的 UninstallString 的代码.

This is the code I used to get the UninstallString of any MSI.

private string GetUninstallString(string msiName)
{
    Utility.WriteLog("Entered GetUninstallString(msiName) - Parameters: msiName = " + msiName);
    string uninstallString = string.Empty;
    try
    {
        string path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products";

        RegistryKey key = Registry.LocalMachine.OpenSubKey(path);

        foreach (string tempKeyName in key.GetSubKeyNames())
        {
            RegistryKey tempKey = key.OpenSubKey(tempKeyName + "\\InstallProperties");
            if (tempKey != null)
            {
                if (string.Equals(Convert.ToString(tempKey.GetValue("DisplayName")), msiName, StringComparison.CurrentCultureIgnoreCase))
                {
                    uninstallString = Convert.ToString(tempKey.GetValue("UninstallString"));
                    uninstallString = uninstallString.Replace("/I", "/X");
                    uninstallString = uninstallString.Replace("MsiExec.exe", "").Trim();
                    uninstallString += " /quiet /qn";
                    break;
                }
            }
        }

        return uninstallString;
    }
    catch (Exception ex)
    {
        throw new ApplicationException(ex.Message);
    }
}

这将给出如下结果:

MsiExec.exe /I{6BB09011-69E1-472F-ACAD-FA0E7DA3E2CE}

从该字符串中,您可以将大括号{}中的子字符串作为 6BB09011-69E1-472F-ACAD-FA0E7DA3E2CE .我希望这可能是产品代码.

From this string, you can take the substring within the braces {}, which will be 6BB09011-69E1-472F-ACAD-FA0E7DA3E2CE. I hope this might be the product code.

这篇关于获取已安装的Msi的产品代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:03