从C#中的msi文件获取产品名称
我有一个安装应用程序的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);
使用此代码不是更容易:
Type type = typeof(Windows.Installer);
如果您更喜欢Type.GetType(String)重载,则必须在完整的类路径后包含正确的程序集名称,例如:
Type type = Type.GetType("Windows.Installer,
你从哪里得到“Windows.Installer”的东西?
…因为:
-
Type.GetType
采用.NET类型名称,而不是COM ProgId。 - Windows Installer(至少在Windows 2003上)没有ProgId。
总结:使用P / Invoke( DllImport
等)与MSI API通信。