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

5 .net c# 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...
...
}
Run Code Online (Sandbox Code Playgroud)

但现在?Type为null,这会引发错误.我在哪里传递MSI文件的名称?

感谢任何提示和评论.

Chr*_*zor 6

你需要使用:

        Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
Run Code Online (Sandbox Code Playgroud)

以下是我的一些代码示例 - 在我的例子中,我得到了安装程序版本:

        // 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);
Run Code Online (Sandbox Code Playgroud)


Rog*_*mbe 1

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

...因为:

  1. Type.GetType采用 .NET 类型名称,而不是 COM ProgId。
  2. Windows Installer(至少在 Windows 2003 上)没有 ProgId。

总之:使用 P/Invoke(DllImport等)与 MSI API 对话。