【发布时间】:2019-01-17 12:15:49
【问题描述】:
我在 GitHub 上找到了一个nice Powershell function,它使用 WindowsInstaller.Installer COM 对象来查询已安装的应用程序,并非常漂亮地列出了所有详细信息和属性。但是,我不想使用Powershell 我想创建一个exe。
我不想搜索注册表,也不想使用 WMI。我想使用与 powershell 脚本和我找到的另一个 vbscript 中使用的完全相同的方法。确实存在一个名为 WindowsInstaller.Installer 的 COM 对象。它确实存在,但由于某种原因,我找不到一个示例,说明在使用 C# 在 Visual Studio 中导入 msi.dll 后如何访问它。
有人知道这个问题的答案吗?
在 Visual Studio 中,为 WindowsInstaller 添加 COM 引用的 WindowsInstaller.Installer 引用只是一种类型,仅此而已。它不包含名为“GetType”的方法,并且尝试将此 PowerShell 转换为 C# 无法正常工作。
我也不知道@{} 是什么意思,但我猜它是指Hashtable。
我在强迫以下情况下的悲伤尝试:
private void Form1_Load(object sender, EventArgs e)
{
Type installerType = Type.GetType("WindowsInstaller.Installer");
Installer installerObj = (Installer)Activator.CreateInstance(installerType);
WindowsInstaller.Installer installer = installerObj as WindowsInstaller.Installer;
var type = installer.GetType();
var Products = type.InvokeMember("Products", System.Reflection.BindingFlags.GetProperty, null, installer, null) as IEnumerable<object>;
foreach (var product in Products)
{
Hashtable hash = new Hashtable();
hash.Add("ProductCode", product);
string[] Attributes = { "Language", "ProductName", "PackageCode", "Transforms", "AssignmentType", "PackageName", "InstalledProductName", "VersionString", "RegCompany", "RegOwner", "ProductID", "ProductIcon", "InstallLocation", "InstallSource", "InstallDate", "Publisher", "LocalPackage", "HelpLink", "HelpTelephone", "URLInfoAbout", "URLUpdateInfo" };
foreach (var attribute in Attributes)
{
object[] thing = { product, attribute };
var details = type.InvokeMember("ProductInfo", System.Reflection.BindingFlags.GetProperty, null, installer, thing);
hash.Add(attribute, details);
}
new ??????????
}
}
【问题讨论】:
标签: c# com windows-installer