You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取MSP文件的版本号?现有MSI版本获取代码对MSP无效

C# 获取MSP补丁包版本号的实现方案

你之前的MSI版本查询代码不适用MSP文件的核心原因是:MSP是Windows Installer补丁包,属于特殊结构的补丁数据库,需要用补丁专属模式打开,且版本信息存储在MsiPatchMetadata表而非Property表中。

完整实现代码

public static string GetMSPVersion(string MSPPath)
{
    try
    {
        Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
        WindowsInstaller.Installer installer = (WindowsInstaller.Installer)Activator.CreateInstance(type);
        // 32对应系统定义的msiOpenDatabaseModePatchFile枚举,为MSP补丁专属打开模式
        WindowsInstaller.Database db = installer.OpenDatabase(MSPPath, 32);
        WindowsInstaller.View dv = db.OpenView("SELECT `Value` FROM `MsiPatchMetadata` WHERE `Property`='PatchVersion'");
        WindowsInstaller.Record record = null;
        dv.Execute(record);
        record = dv.Fetch();
        string version = record.get_StringData(1).ToString();
        // 释放COM资源避免内存泄漏
        System.Runtime.InteropServices.Marshal.ReleaseComObject(record);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(dv);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(db);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(installer);
        return version;
    }
    catch (Exception ex)
    {
        return "";
    }
}

注意事项

  • 该方法依赖的COM组件和读取MSI的依赖完全一致,不需要额外引入其他库
  • 部分自定义打包的MSP补丁可能未按照规范存储PatchVersion属性,若读取为空可以将SQL查询语句修改为SELECT Property, ValueFROMMsiPatchMetadata``遍历所有属性,找到对应版本信息的字段
  • 代码中增加了COM资源释放逻辑,避免高频调用时出现内存泄漏问题

内容的提问来源于stack exchange,提问作者Andrii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 21:27:01