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

如何使用Node.js读取MSI文件的注释及安装包内置软件版本?

读取MSI文件注释/版本号的Node.js实现方案

你要找的「控制面板卸载界面显示的MSI版本号」并不存储在通用文件元数据字段里,而是打包在MSI安装包内部的结构化Property表中,字段名一般为ProductVersion,你之前找的图片、MP3元数据读取库都是针对特定媒体格式设计的,无法解析MSI的包内数据,以下是可落地的实现方案:


跨平台方案(支持Windows/Linux/macOS)

纯JS实现,不需要依赖系统原生工具,Linux环境可直接运行,优先推荐这个方案。

实现步骤

  1. 安装对应依赖
npm install msi-parser
  1. 调用示例代码
const { parseMSI } = require('msi-parser');
const fs = require('fs');

async function getMsiInfo(msiPath) {
  // 读取MSI文件到缓冲区
  const msiBuffer = fs.readFileSync(msiPath);
  // 解析MSI内部结构
  const msiData = await parseMSI(msiBuffer);
  // 读取控制面板显示的版本号
  const productVersion = msiData.tables.Property.find(item => item.Property === 'ProductVersion')?.Value;
  // 读取包内存储的注释内容(如果你的注释存在自定义字段里可以对应修改查找规则)
  const comment = msiData.tables.Property.find(item => item.Property === 'Comment')?.Value;
  return { productVersion, comment };
}

// 替换为你的MSI文件路径即可调用
getMsiInfo('./test-installer.msi').then(console.log);

Windows专属方案(无额外npm包依赖)

如果你只需要在Windows环境运行,可以直接调用系统自带的WMI工具实现,不需要安装第三方依赖:

const { exec } = require('child_process');

function getMsiVersionWin(msiPath) {
  return new Promise((resolve, reject) => {
    // 注意处理路径里的特殊字符转义
    exec(`wmic datafile where name="${msiPath.replace(/\\/g, '\\\\')}" get Version`, (err, stdout) => {
      if (err) return reject(err);
      const version = stdout.trim().split('\n')[1]?.trim();
      resolve(version);
    });
  });
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 09:39:02