WiX从MSI读取ProductName和ProductVersion

时间:2014-05-08 15:09:01

标签: wix windows-installer

我有一个MSI文件,我需要从中读取ProductName和ProductVersion信息。这是的SummaryInfo数据。

我正在使用Microsoft.Deployment.WindowsInstaller并拥有以下代码:

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"));
            }
        }
    }
}

这可以很好地枚举所有属性/值对,但我只需要ProductNameProductVersion添加到Tuple<string, string>。如何从MSI中读取这两个值?

更新

我做到了这一点,它确实有效,但可能有一个更有效/更有效的解决方案:

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 个答案:

答案 0 :(得分:0)

我做到了这一点,它有效,但可能有一个更简洁/有效的解决方案:

    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;
    }