Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C中的msi文件获取产品名称#_C#_.net_Windows Installer - Fatal编程技术网

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

C# 从C中的msi文件获取产品名称#,c#,.net,windows-installer,C#,.net,Windows Installer,我有一个安装应用程序的msi文件。在安装开始之前,我需要知道该应用程序的产品名称 我尝试了以下方法: { ... Type type = Type.GetType("Windows.Installer"); WindowsInstaller.Installer installer = (WindowsInstaller.Installer) Activator.CreateInstance(type); installer.OpenDatabase(msiFile, 0); //this

我有一个安装应用程序的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文件的名称

谢谢您的提示和评论。

您从哪里获得“Windows.Installer”的资料

…因为:

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

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

    使用此代码不是更容易吗:

    Type-Type=typeof(Windows.Installer)

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

    Type Type=Type.GetType(“Windows.Installer”)

    您需要使用:

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

    太好了,我不知道P/Invoke是什么;我没有在网上找到一个可运行的代码,这似乎是一个备受尊重的秘密。因此,这可能是一个解决方案,但目前我或多或少地以一些DllImport结束。请把我当作这件事的新手。谢谢你的回答。我没有偏好,我只是在网上搜索,希望找到解决问题的方法。“MSI安装程序的汇编”是什么意思?我只有msi文件。但是“typeof”方法似乎也不起作用(类型安装程序不存在)。如果“Windows.Installer”不在mscorlib或当前正在执行的程序集中,则必须在type.GetType(字符串)中指定程序集的名称。typeof()中有一个输入错误。它应该是typeof(WindowsInstaller)而不带点。有没有不使用COM的解决方案?有关于它的最终完整源代码吗?