WiX Read ProductName and ProductVersion from MSI

2019-05-31 10:49发布

问题:

I have an MSI file from which I need to read the ProductName and ProductVersion info. This is not the SummaryInfo data.

I am using Microsoft.Deployment.WindowsInstaller and have the following code:

using (var database = new Database(msi, DatabaseOpenMode.ReadOnly))
{
  using (var view = database.OpenView(database.Tables["Property"].SqlSelectString))
    {
        view.Execute();
        foreach (var rec in view)
        {
            using (rec)
            {
                Console.WriteLine("{0} = {1}", rec.GetString("Property"), rec.GetString("Value"));
            }
        }
    }
}

This does a fine job of enumerating all the Property/Value pairs, but I just need ProductName and ProductVersion to add to a Tuple<string, string>. How can I read just these two values from the MSI?

UPDATE

I got this far, and it works, but there is probably a more succint/efficient solution:

public static IEnumerable<Tuple<string, string>> GetVersionInfo()
{
    // Enumerate MSI files in C:\SetupFiles (or wherever)
    var setupFilesDir = ConfigurationManager.AppSettings["setupFilesDir"] ?? string.Empty;
    var di = new DirectoryInfo(setupFilesDir);
    var msiFiles = from msi in di.GetFiles("*.msi", SearchOption.AllDirectories)
        select msi.FullName;

    var tuples = new List<Tuple<string, string>>();

    foreach (var msi in msiFiles)
    {
        using (var db = new Database(msi, DatabaseOpenMode.ReadOnly))
        {
            var productVersion = (string)db.ExecuteScalar("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductVersion'");
            var productName = (string)db.ExecuteScalar("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductName'");

            tuples.Add(new Tuple<string, string>(productName, productVersion));
        }
    }

    return tuples;
}

回答1:

I got this far, and it works, but there is probably a more succint/efficient solution:

    public static IEnumerable<Tuple<string, string>> GetVersionInfo()
    {
        // Enumerate MSI files in C:\SetupFiles (or wherever)
        var setupFilesDir = ConfigurationManager.AppSettings["setupFilesDir"] ?? string.Empty;
        var di = new DirectoryInfo(setupFilesDir);
        var msiFiles = from msi in di.GetFiles("*.msi", SearchOption.AllDirectories)
            select msi.FullName;

        var tuples = new List<Tuple<string, string>>();

        foreach (var msi in msiFiles)
        {
            using (var db = new Database(msi, DatabaseOpenMode.ReadOnly))
            {
                var productVersion = (string)db.ExecuteScalar("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductVersion'");
                var productName = (string)db.ExecuteScalar("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductName'");

                tuples.Add(new Tuple<string, string>(productName, productVersion));
            }
        }

        return tuples;
    }