寺委书记

Good good study, day day up!

导航

检测MSI安装包的版本信息

Posted on 2011-01-17 09:40  MonkChen  阅读(777)  评论(0)    收藏  举报

原文 http://www.alteridem.net/2008/05/20/read-properties-from-an-msi-file/

It took a fair amount of searching and code tweaking, but I finally worked it all out.

  1. Add a reference to the COM Microsoft Windows Installer Object Library.
  2. Add a using WindowsInstaller;
  3. Add the following static method to your code (error checking removed for brevity.)
     public static string GetMsiProperty(string msiFile, string property)
            {
                
    string retVal = string.Empty;

                
    // Create an Installer instance   
                Type classType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
                Object installerObj 
    = Activator.CreateInstance(classType);
                Installer installer 
    = installerObj as Installer;

                
    // Open the msi file for reading   
                
    // 0 - Read, 1 - Read/Write   
                Database database = installer.OpenDatabase(msiFile, 0);

                
    // Fetch the requested property   
                string sql = String.Format("SELECT Value FROM Property WHERE Property='{0}'", property);
                WindowsInstaller.View view 
    = database.OpenView(sql);
                view.Execute(
    null);

                
    // Read in the fetched record   
                Record record = view.Fetch();
                
    if (record != null)
                    retVal 
    = record.get_StringData(1);
            view.Close();  
                
    return retVal;
            } 

     

4.使用

GetMsiProperty(openFileDialog1.FileName, "ProductVersion");

 

另外还有差不多的一篇:

http://www.codeproject.com/KB/install/Read_MSI.aspx