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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/650866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:50:18  来源:igfitidea点击:

Get product name from msi file in C#

c#.netwindows-installer

提问by

I have an msi file that installs an application. I need to know the product name of that application beforethe installation starts.

我有一个安装应用程序的 msi 文件。在安装开始之前,我需要知道该应用程序的产品名称。

I tried the following:

我尝试了以下方法:

{ 

...
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...
...
}

but now? Type is null, which throws me an error. And where do I pass in the name of the MSI file?

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

Thanks for any hints & comments.

感谢您的任何提示和评论。

回答by Roger Lipscombe

Where did you get the "Windows.Installer" stuff from?

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

...because:

...因为:

  1. Type.GetTypetakes a .NET type name, not a COM ProgId.
  2. Windows Installer (at least on Windows 2003) doesn't have a ProgId.
  1. Type.GetType采用 .NET 类型名称,而不是 COM ProgId。
  2. Windows Installer(至少在 Windows 2003 上)没有 ProgId。

In summary: Use P/Invoke (DllImport, etc.) to talk to the MSI API.

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

回答by Jozef Izso

Wouldn't it be easier to use this code:

使用此代码不是更容易:

Type type = typeof(Windows.Installer);

Type type = typeof(Windows.Installer);

If you prefer the Type.GetType(String) overload you must include correct assembly name after full path to class, eg.:

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

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

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

回答by Chris Kaczor

You need to use:

您需要使用:

        Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");

Here is a sample from some of my code - in my case I get the installer version:

这是我的一些代码的示例 - 在我的情况下,我获得了安装程序版本:

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