How to get msp version?

236 Views Asked by At

I need to get version of msp-files. For msi-files, I use next code:

 public static string GetMSIVersion(string MSIPath)
 {
    try
    {
        Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
        WindowsInstaller.Installer installer = (WindowsInstaller.Installer)
        Activator.CreateInstance(type);
        WindowsInstaller.Database db = installer.OpenDatabase(MSIPath, 0);
        WindowsInstaller.View dv = db.OpenView("SELECT `Value` FROM `Property` WHERE `Property`='ProductVersion'");
        WindowsInstaller.Record record = null;
        dv.Execute(record);
        record = dv.Fetch();
        string str = record.get_StringData(1).ToString();
       return str;
    }
    catch (Exception ex)
    {
        return "";
    }
}

But for msp it's not working. Any ideas?

1

There are 1 best solutions below

0
On

You need to specify MSP database type when OpenDatabase, replace 0 with MsiOpenDatabaseMode.msiOpenDatabaseModePatchFile (32)

Then you can receive all tables inside the msp:

Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
WindowsInstaller.Installer installer = (WindowsInstaller.Installer)Activator.CreateInstance(installerType);
Database database = installer.OpenDatabase(mspPath, 
MsiOpenDatabaseMode.msiOpenDatabaseModePatchFile);
View view = database.OpenView("SELECT * FROM `_Tables`");

view.Execute(null);
Record record = view.Fetch();
while (record != null)
{
     Console.WriteLine(record.StringData[1]);
     record = view.Fetch();
}

It should contain patch tables listed there. MSP file may not contain Property table inside .