确定第三方应用程序安装目录

我有一个应用程序,在整个公司的几百台计算机上使用,我必须修改应用程序的安装目录中的INI文件。 用户可以随意安装应用程序,并且可以在任何给定时间安装多个版本的应用程序。 我需要能够找到该安装目录。

到目前为止我考虑过的方法:

  • 使用WindowsInstaller按名称查找产品并查找其安装目录。 (从这里 )。 – 这几乎可以工作,但我期望填充的属性(TARGETDIR,APPDIR)不是。
  • 查看注册表以查找特定应用程序的安装目录。 它不在那里。
  • MsiGetComponentPath()? 我在上面提到的相同链接中看到了这个,但我不知道如何实现它。 我可以使用Windows安装程序获取ProductID,但我不知道如何以编程方式选择一个组件并随机查找其ID。 任何人?

好吧,让我们听一下以编程方式确定Windows应用程序的安装目录的任何其他方法。

我想出了一个适合我的解决方案:

Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer"); Installer msi = (Installer)Activator.CreateInstance(type); foreach (string productcode in msi.Products) { string productname = msi.get_ProductInfo(productcode, "InstalledProductName"); if (productname.Contains("")) { string installdir = msi.get_ProductInfo(productcode, "InstallLocation"); } } 

使用WMI可能适用于某些人,遗憾的是我们的用户将无法使用凭据允许他们在自己的计算机上执行此操作:

  ManagementObjectSearcher search = new ManagementObjectSearcher("Select InstallationLocation from Win32_Product"); ManagementObjectCollection results = search.Get(); foreach (ManagementObject mo in results) { Console.WriteLine(mo["InstallLocation"]); } 

如果安装是MSI,那么从WMI获取信息是微不足道的。 Win32_Product类具有InstallLocation属性来保存此信息。