C# 获取msi文件的ProductCode

private string GetProductCode()
{
System.Type oType
= System.Type.GetTypeFromProgID("WindowsInstaller.Installer");
Installer inst
= System.Activator.CreateInstance(oType) as Installer;
Database DB
= inst.OpenDatabase("msi文件完整路径", MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
string str = "SELECT * FROM Property WHERE Property = 'ProductCode'";

WindowsInstaller.View thisView
= DB.OpenView(str);
thisView.Execute();
WindowsInstaller.Record thisRecord
= thisView.Fetch();
string result = thisRecord.get_StringData(2);

return result;
}

注意添加WindowsInstaller的引用:c:\windows\sytem32\msi.dll。

原文地址:https://www.cnblogs.com/miaochw/p/2076442.html