private string QueryDB(Database msiDatabase, string strProperty) { string result = null; /* * string sql = "SELECT Property, Value FROM Property"; * WindowsInstaller.View view = msiDatabase.OpenView(sql); * view.Execute(null); * * Record record = view.Fetch(); * do * { * String value = record.get_StringData(1); * if (String.Compare(value, strProperty, StringComparison.OrdinalIgnoreCase) == 0) * { * result = record.get_StringData(2); * } * record = view.Fetch(); * } while (record != null); */ string sql = "SELECT Value FROM Property WHERE Property = '" + strProperty + "'"; WindowsInstaller.View view = msiDatabase.OpenView(sql); view.Execute(null); Record record = view.Fetch(); if (record != null) { result = record.get_StringData(1); } view.Close(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(view); return(result); }
//获取MSI文件的版本号 private static String getMsiFileVersion(String msiPath) { System.Type oType = System.Type.GetTypeFromProgID("WindowsInstaller.Installer"); Installer inst = System.Activator.CreateInstance(oType) as Installer; Database DB = inst.OpenDatabase(msiPath, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly); string str = "SELECT * FROM Property WHERE Property = 'ProductVersion'"; //读取文件版本号 string ProductVersion = ""; if (str != "") { WindowsInstaller.View thisView = DB.OpenView(str); thisView.Execute(); WindowsInstaller.Record thisRecord = thisView.Fetch(); ProductVersion = thisRecord.get_StringData(2); //获取特定的数据列 } Console.WriteLine("文件版本号:" + ProductVersion); //获取失败 //FileVersionInfo fvi = FileVersionInfo.GetVersionInfo("E:\\Users\\MVP\\Desktop\\ApplicationLibrary\\MSI\\腾讯QQ.msi"); //Console.WriteLine("文件版本号:" + fvi.FileVersion); return(ProductVersion); }
/// <summary> /// Get an MSI Property /// </summary> /// <param name="sProperty">MSI Property Name</param> /// <returns>Value of an MSI Property</returns> public string Property(string sProperty) { try { WindowsInstaller.View vMSI = msiDatabase.OpenView("SELECT * FROM Property"); vMSI.Execute(null); Record iRec = vMSI.Fetch(); while (iRec != null) { if (string.Compare(iRec.get_StringData(1), sProperty, true) == 0) { return(iRec.get_StringData(2)); } iRec = vMSI.Fetch(); } return(""); } catch (Exception ex) { return(""); } finally { GC.Collect(); GC.WaitForPendingFinalizers(); } }
private void FillColumnsOrder(SortedDictionary <string, Table> tables) { try { // Open a view on the Property table for the version property string request = "SELECT * FROM _Columns"; WindowsInstaller.View view = this._database.OpenView(request); // Execute the view query view.Execute(null); // Get the record from the view Record record = view.Fetch(); while (record != null) { string tableName = record.StringData[1]; int number = record.IntegerData[2]; string columnName = record.StringData[3]; tables[tableName].GetColumn(columnName).Order = number; tables[tableName].IsOrdered = true; record = view.Fetch(); } view.Close(); } catch (Exception) { } }
private string getCustomActionPathSet(String varName) //TODO: resolve the condition field as custom action might not be used { String resPath = null; WindowsInstaller.View view = null; try { view = database.OpenView("SELECT * FROM `CustomAction` where Source='" + varName + "'"); view.Execute(); } catch (Exception ex) { Console.WriteLine("Unable to query msi, " + ex.Message); } Record properties = view.Fetch(); while (properties != null) { int setPropActio = properties.get_IntegerData(2) & 51; if (setPropActio == 51) { resPath = properties.get_StringData(4); } properties = view.Fetch(); } Console.WriteLine("51 " + varName + "--" + resPath); return(resPath); }
public string QueryMSI(string fileLocation, string query) { string result = string.Empty; FileInfo msiFile = new FileInfo(fileLocation); //Hashtable msiData = new Hashtable(); WindowsInstaller.Installer inst = (WindowsInstaller.Installer) new Installer(); try { Database instDb = inst.OpenDatabase(msiFile.FullName, WindowsInstaller.MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly); WindowsInstaller.View view = instDb.OpenView(query); view.Execute(null); Record record = view.Fetch(); //string fileName = record.get_StringData(1); // record = view.Fetch(); result = record.get_StringData(1); view.Close(); } catch (Exception ex) { } return(result); }
/// <summary> /// Retrieves the value of a property. /// </summary> /// <param name="PropertyName">Name of the Property to query.</param> /// <returns>Return a string which contain the result of the query. If an error occurs, return an empty string.</returns> internal string RetrievePropertyValue(string PropertyName) { string result = string.Empty; WindowsInstaller.View view = null; try { // Open a view on the Property table for the version property view = this._database.OpenView($"SELECT * FROM Property WHERE Property='{PropertyName}'"); // Execute the view query view.Execute(null); // Get the record from the view Record record = view.Fetch(); result = record.get_StringData(2); } catch (Exception) { } finally { view?.Close(); } return(result); }
private Record getMsiRecord(string query) { Installer installer = (Installer)Activator.CreateInstance( Type.GetTypeFromProgID("WindowsInstaller.Installer")); Database database = installer.OpenDatabase(SetupFilePath, 0); WindowsInstaller.View view = database.OpenView(query); view.Execute(null); return(view.Fetch()); }
public MSIOps(String msiP) { InitializeComponent(); logDirectory.Text = Settings.LOGGING_DIR + "\\MSI\\"; installer.Text = msiP; Type t = Type.GetTypeFromProgID("WindowsInstaller.Installer"); Installer inst = (Installer)Activator.CreateInstance(t); Database d = inst.OpenDatabase(msiP, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly); WindowsInstaller.View v = d.OpenView("SELECT * FROM Property WHERE Property = 'ProductName'"); v.Execute(null); Record r = v.Fetch(); MSIProductName.Text = r.get_StringData(2); v = d.OpenView("SELECT * FROM Property WHERE Property = 'ProductVersion'"); v.Execute(null); r = v.Fetch(); MSIPVersion.Text = r.get_StringData(2); }
static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Incorrect args."); return; } //arg 1 - path to MSI string PathToMSI = args[0]; //arg 2 - path to assembly string PathToAssembly = args[1]; Type InstallerType; WindowsInstaller.Installer Installer; InstallerType = Type.GetTypeFromProgID("WindowsInstaller.Installer"); Installer = (WindowsInstaller.Installer)Activator.CreateInstance(InstallerType); Assembly Assembly = Assembly.LoadFrom(PathToAssembly); string AssemblyStrongName = Assembly.GetName().FullName; string AssemblyVersion = Assembly.GetName().Version.ToString(); string SQL = "SELECT `Key`, `Name`, `Value` FROM `Registry`"; WindowsInstaller.Database Db = Installer.OpenDatabase(PathToMSI, WindowsInstaller.MsiOpenDatabaseMode.msiOpenDatabaseModeDirect); WindowsInstaller.View View = Db.OpenView(SQL); View.Execute(); WindowsInstaller.Record Rec = View.Fetch(); while (Rec != null) { for (int c = 0; c <= Rec.FieldCount; c++) { string Column = Rec.get_StringData(c); Column = Column.Replace("[AssemblyVersion]", AssemblyVersion); Column = Column.Replace("[AssemblyStrongName]", AssemblyStrongName); Rec.set_StringData(c, Column); View.Modify(MsiViewModify.msiViewModifyReplace, Rec); Console.Write("{0}\t", Column); Db.Commit(); } Console.WriteLine(); Rec = View.Fetch(); } View.Close(); GC.Collect(); Marshal.FinalReleaseComObject(Installer); Console.ReadLine(); }
private WindowsInstaller.View queryMsi(String query) { WindowsInstaller.View view = null; try { view = database.OpenView(query); view.Execute(); } catch (Exception ex) { Console.WriteLine("Unable to query msi, " + ex.Message); } return(view); }
private DataTable QueryMSIFileandVersions(string query) { DataTable dt = new DataTable(); dt.Columns.Add("Filename"); dt.Columns.Add("Version"); FileInfo msiFile = new FileInfo(_mSIfileLocation); //Hashtable msiData = new Hashtable(); WindowsInstaller.Installer inst = (WindowsInstaller.Installer) new Installer(); try { Database instDb = inst.OpenDatabase(msiFile.FullName, WindowsInstaller.MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly); WindowsInstaller.View view = instDb.OpenView(query); view.Execute(null); Record record = view.Fetch(); while (record != null) { DataRow dr = dt.NewRow(); dr[0] = record.get_StringData(1); dr[1] = record.get_StringData(2); dt.Rows.Add(dr); record = view.Fetch(); } // close the database view.Close(); } catch (Exception ex) { } return(dt); }
/// <summary> /// Query the requested Table from the MSI file and return all properties with their values. /// </summary> /// <param name="table">Name of the MSI Table to query.</param> /// <returns>Return a Dictionnary with property name as key and property value as value.</returns> internal Table GetAllMSIValuesFromTable(Table table) { WindowsInstaller.View view = null; try { string columnsName = String.Empty; foreach (Column column in table.Columns) { columnsName += $"{column.Name}, "; column.Values.Clear(); } columnsName = columnsName.Substring(0, columnsName.LastIndexOf(",")); // Open a view on the Property table for the version property view = this._database.OpenView($"SELECT {columnsName} FROM {table.Name}"); // Execute the view query view.Execute(null); // Get the record from the view Record record = view.Fetch(); while (record != null) { int i = 1; foreach (Column column in table.Columns) { column.Values.Add(record.StringData[i]); i++; } record = view.Fetch(); } } catch (Exception) { } finally { view?.Close(); } return(table); }
/// <summary> /// Msi ファイルからアンインストール時に必要な情報を取得するメソッド /// /// 参考1: Read Properties from an MSI File /// http://www.alteridem.net/2008/05/20/read-properties-from-an-msi-file/ /// /// 参考2: 1で COM Microsoft Windows Installer Object Library を参照しようとした時のエラー回避方法 /// %WINDIR%\system32\msi.dll をかわりに追加するとよい /// http://www.reinholdt.me/2015/07/29/problem-adding-a-c-reference-to-windowsinstaller-com-object/ /// /// 参考3: Windows SDK Components for Windows Installer Developers に含まれる View Installer Script の解説 /// https://docs.microsoft.com/en-us/windows/desktop/Msi/view-installer-script /// /// </summary> /// <param name="msiFile"></param> /// <param name="property"></param> /// <returns></returns> public static string GetMsiProperty(string msiFile, string property) { string retVal = string.Empty; // インストーラのインスタンスを作成 Type classType = Type.GetTypeFromProgID("WindowsInstaller.Installer"); Object installerObj = Activator.CreateInstance(classType); Installer installer = installerObj as Installer; // 読み込みのために msi ファイルをオープン // OpneDatabase method // 0 - Read, 1 - Read/Write Database database = installer.OpenDatabase(msiFile, 0); // SQL文でプロパティをクエリして取得 // OpneView method string sql = String.Format( "SELECT Value FROM Property WHERE Property ='{0}'", property); WindowsInstaller.View view = database.OpenView(sql); // Execute method and Fetch method view.Execute(null); // 取得したレコードの読み込み Record record = view.Fetch(); // StringData property if (record != null) { // プロパティの値を返す //retVal = string.Format("{0}: {1}", property, record.get_StringData(1)); // アンインストール用のコマンドの形式で返す // mxiexec /X <PruductGUID> retVal = string.Format("mxiexec /X '{0}'", record.get_StringData(1)); } retVal = retVal.Replace("'", "\""); return(retVal); }
private void parseDirectory() { WindowsInstaller.View view = null; try { view = database.OpenView("SELECT * FROM `Directory`"); view.Execute(); } catch (Exception ex) { Console.WriteLine("Unable to query msi, " + ex.Message); } directoryTable = new Hashtable(); Record properties = view.Fetch(); while (properties != null) { directoryTable.Add(properties.get_StringData(1).ToLower(), new MsiDirectory(properties.get_StringData(1), properties.get_StringData(2), properties.get_StringData(3))); properties = view.Fetch(); } }
private Dictionary <string, string> QueryMSI(string query) { Dictionary <string, string> FilesAndVersion = new Dictionary <string, string>(); FileInfo msiFile = new FileInfo(_mSIfileLocation); //Hashtable msiData = new Hashtable(); WindowsInstaller.Installer inst = (WindowsInstaller.Installer) new Installer(); try { Database instDb = inst.OpenDatabase(msiFile.FullName, WindowsInstaller.MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly); WindowsInstaller.View view = instDb.OpenView(query); view.Execute(null); Record record = view.Fetch(); while (record != null) { //string fileName = record.get_StringData(1); record = view.Fetch(); FilesAndVersion.Add(record.get_StringData(1), record.get_StringData(2)); } // close the database view.Close(); } catch (Exception ex) { } return(FilesAndVersion); }
private static string GetMsiVersion(string fullFile) { Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer"); // Create the Windows Installer object Installer installer = (Installer)Activator.CreateInstance(installerType); // Open the MSI database in the input file Database database = installer.OpenDatabase(fullFile, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly); // Open a view on the Property table for the version property WindowsInstaller.View view = database.OpenView("SELECT * FROM Property WHERE Property = 'ProductVersion'"); // Execute the view query view.Execute(null); // Get the record from the view Record record = view.Fetch(); // Get the version from the data return(record.get_StringData(2)); }
private void parseProperties() { WindowsInstaller.View view = null; try { view = database.OpenView("SELECT * FROM `Property`"); view.Execute(); } catch (Exception ex) { Console.WriteLine("Unable to query msi, " + ex.Message); } msiProperties = new MsiProperties(); Record properties = view.Fetch(); while (properties != null) { switch (properties.get_StringData(1).ToLower()) { case "manufacturer": msiProperties.Manufacturer = properties.get_StringData(2); break; case "productname": msiProperties.ProductName = properties.get_StringData(2); break; case "arpcomments": msiProperties.ArpComments = properties.get_StringData(2); break; case "arphelplink": msiProperties.ArpHelpLink = properties.get_StringData(2); break; case "arpcontact": msiProperties.ArpContact = properties.get_StringData(2); break; case "allusers": msiProperties.AllUsers = properties.get_StringData(2); break; case "arpinstalllocation": msiProperties.ArpInstallLocation = properties.get_StringData(2); break; case "arptelephone": msiProperties.ArpTelephone = properties.get_StringData(2); break; case "primaryfolder": msiProperties.PrimaryFolder = properties.get_StringData(2); break; case "msiinstallperuser": msiProperties.MsiInstallPerUser = properties.get_StringData(2); break; case "reboot": msiProperties.Reboot = properties.get_StringData(2); break; case "rootdrive": msiProperties.RootDrive = properties.get_StringData(2); break; case "targetdir": msiProperties.TargetDir = properties.get_StringData(2); break; case "installdir": msiProperties.InstallDir = properties.get_StringData(2); break; case "upgradecode": msiProperties.UpgradeCode = properties.get_StringData(2); break; case "productcode": msiProperties.ProductCode = properties.get_StringData(2); break; case "productversion": msiProperties.ProductVersion = properties.get_StringData(2); break; case "productlanguage": msiProperties.ProductLanguage = properties.get_StringData(2); break; } properties = view.Fetch(); } }
/// <summary> /// Return the list of all Tables in this MSI file. /// </summary> /// <returns>List of all Table name.</returns> internal SortedDictionary <string, Table> GetAllMSITables() { WindowsInstaller.View view = null; try { // Open a view on the Property table for the version property view = this._database.OpenView("SELECT * FROM _Validation"); // Execute the view query view.Execute(null); // Get the record from the view Record record = view.Fetch(); while (record != null) { string tableName = record.StringData[1]; string columnName = record.StringData[2]; string nullable = record.StringData[3]; int? minValue = null; if (!string.IsNullOrEmpty(record.StringData[4])) { minValue = record.IntegerData[4]; } int?maxValue = null; if (!string.IsNullOrEmpty(record.StringData[5])) { maxValue = record.IntegerData[5]; } string keyTable = record.StringData[6]; short? keyColumn = null; if (!string.IsNullOrEmpty(record.StringData[7])) { keyColumn = (short)record.IntegerData[7]; } string category = record.StringData[8]; string set = record.StringData[9]; string description = record.StringData[10]; Column column = new Column(columnName, nullable, minValue, maxValue, keyTable, keyColumn, category, set, description); if (_tables.ContainsKey(tableName)) { _tables[tableName].Columns.Add(column); } else { Table table = new Table { Name = tableName }; table.Columns.Add(column); _tables.Add(tableName, table); } record = view.Fetch(); } } catch (Exception) { } finally { view?.Close(); } FillColumnsOrder(_tables); return(_tables); }