从C#中的msi文件获取产品名称

时间:2009-03-16 15:15:58

标签: c# .net windows-installer

我有一个安装应用程序的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...
...
}

但现在呢?键入 null ,这会引发错误。我在哪里传递MSI文件的名称?

感谢任何提示&评价。

3 个答案:

答案 0 :(得分:6)

您需要使用:

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

答案 1 :(得分:2)

使用此代码会不会更容易:

Type type = typeof(Windows.Installer);

如果您更喜欢Type.GetType(String)重载,则必须在完整的类路径后包含正确的程序集名称,例如:

Type type = Type.GetType("Windows.Installer, <assembly for MsiInstaller>");

答案 2 :(得分:1)

你从哪里获得“Windows.Installer”的东西?

...因为:

  1. Type.GetType采用.NET类型名称,而不是COM ProgId。
  2. Windows Installer(至少在Windows 2003上)没有ProgId。
  3. 总结:使用P / Invoke(DllImport等)与MSI API对话。