获取已安装的MSI的产品代码

本文关键字:代码 MSI 安装 获取 | 更新日期: 2023-09-27 17:59:02

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

获取已安装的MSI的产品代码

这个问题的答案有帮助吗?他们想要获取产品名称,但也许它也适用于产品代码?

编辑
如果您没有 MSI 文件本身来访问数据库(如上述指向另一个问题的链接所建议的那样(,您可以尝试在以下注册表路径中搜索 MSI 文件的名称:

HKEY_CLASSES_ROOT'Installer'Products'*'SourceList 

Products分支下有许多条目。它们中的每一个都是一个产品密钥。每个分支都应包含SourceList节点,而该节点又应包含值 PackageName 。该值保存 MSI 文件的名称。

所以我要做的是:

for each key in Products
{
    open SourceList subkey
    read PackageName value
    if name equals my msi file name
    {
        return key-name formatted as GUID
    }
}
这是我

用来获取任何MSI UninstallString的代码。

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 。我希望这可能是产品代码。

有最快速和最简单的方法 - 将 WMI 与条件查询字符串一起使用。

    public string GetProductCode(string productName)
    {
        string query = string.Format("select * from Win32_Product where Name='{0}'", productName);
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        {
            foreach (ManagementObject product in searcher.Get())
                return product["IdentifyingNumber"].ToString();
        }
        return null;
    }

此代码直接从 MSI 文件获取产品代码。因此,这允许在不安装文件的情况下读取代码。

class MsiHandle : SafeHandleMinusOneIsInvalid
{
    public MsiHandle()
        : base(true)
    { }
    protected override bool ReleaseHandle()
    {
        return NativeMethods.MsiCloseHandle(handle) == 0;
    }
}
class NativeMethods
{
    const string MsiDll = "Msi.dll";
    [DllImport(MsiDll, CharSet = CharSet.Unicode, ExactSpelling = true)]
    public extern static uint MsiOpenPackageW(string szPackagePath, out MsiHandle product);
    [DllImport(MsiDll, ExactSpelling=true)]
    public extern static uint MsiCloseHandle(IntPtr hAny);
    [DllImport(MsiDll, CharSet = CharSet.Unicode, ExactSpelling = true)]
    static extern uint MsiGetProductPropertyW(MsiHandle hProduct, string szProperty, StringBuilder value, ref int length);

    [DllImport(MsiDll, ExactSpelling = true)]
    public static extern int MsiSetInternalUI(int value, IntPtr hwnd);
    public static uint MsiGetProductProperty(MsiHandle hProduct, string szProperty, out string value)
    {
        StringBuilder sb = new StringBuilder(1024);
        int length = sb.Capacity;
        uint err;
        value = null;
        if(0 == (err = MsiGetProductPropertyW(hProduct, szProperty, sb, ref length)))
        {
            sb.Length = length;
            value = sb.ToString();
            return 0;
        }
        return err;
    }
}
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static int Main(string[] args)
    {
        string msiFile = args[0];
        NativeMethods.MsiSetInternalUI(2, IntPtr.Zero); // Hide all UI. Without this you get a MSI dialog
        MsiHandle msi;
        uint err;
        if (0 != (err = NativeMethods.MsiOpenPackageW(args[0], out msi)))
        {
            Console.Error.WriteLine("Can't open MSI, error {0}", err);
            return 1;
        }
        // Strings available in all MSIs
        string productCode;
        using (msi)
        {
           if (0 != NativeMethods.MsiGetProductProperty(msi, "ProductCode", out productCode))
                throw new InvalidOperationException("Can't obtain product code");
           Console.WriteLine(productCode);
           return 0;
        }
    }
}

Subversion on http://ankhsvn.open.collab.net/svn/ankhsvn/trunk/src/tools/Ankh.Chocolatey/中的完整示例使用用户名"guest",不使用密码。

private static bool GetUninstallString(string ProductName)
{
    try
    {
        RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
        var key = localKey.OpenSubKey(@"SOFTWARE'Microsoft'Windows'CurrentVersion'Uninstall") ??
            localKey.OpenSubKey(
                @"SOFTWARE'Wow6432Node'Microsoft'Windows'CurrentVersion'Uninstall");
        if (key == null)
                return false;
        return key.GetSubKeyNames()
                .Select(keyName => key.OpenSubKey(keyName))
                .Select(subkey => subkey.GetValue("DisplayName") as string)
                .Any(displayName => displayName != null && displayName.Contains(ProductName));
     }
     catch
     {
         // Log message                  
         return false;
     }
 }

这对于按产品名称搜索字符串非常有用