C# 从C中的msi文件获取产品名称#
我有一个安装应用程序的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
{
...
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总之:使用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的解决方案?有关于它的最终完整源代码吗?