我有一个安装应用程序的msi文件.我需要在安装开始之前知道该应用程序的产品名称.
我尝试了以下方法:
{ ... Type type = Type.GetType("Windows.Installer"); WindowsInstaller.Installer installer = (WindowsInstaller.Installer) Activator.CreateInstance(type); installer.OpenDatabase(msiFile, 0); //this is my guess to pass in the msi file name... ... }
但现在?Type为null,这会引发错误.我在哪里传递MSI文件的名称?
感谢任何提示和评论.
你需要使用:
Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
以下是我的一些代码示例 - 在我的例子中,我得到了安装程序版本:
// Get the type of the Windows Installer object 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(inputFile, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly); // Open a view on the Property table for the version property 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 string version = record.get_StringData(2);