安装期间卸载应用程序无法正常工作

我正在用C#开发WPF应用程序。 目前我的msi在机器上安装了当前的应用程序。我需要在安装新的一个(msi)时卸载现有版本的两个支持应用程序。

我编写代码以编程方式卸载应用程序,当我在installer.cs调用应用程序卸载方法时,这不起作用。当我从除installer.cs以外的项目的其他部分调用时,同样的方法卸载两个应用installer.cs

卸载方法:

 public static string GetUninstallCommandFor(string productDisplayName) { RegistryKey localMachine = Registry.LocalMachine; string productsRoot = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"; RegistryKey products = localMachine.OpenSubKey(productsRoot); string[] productFolders = products.GetSubKeyNames(); foreach (string p in productFolders) { RegistryKey installProperties = products.OpenSubKey(p + @"\InstallProperties"); if (installProperties != null) { string displayName = (string)installProperties.GetValue("DisplayName"); if ((displayName != null) && (displayName.Contains(productDisplayName))) { string fileName = "MsiExec.exe"; string arguments = "/x{4F6C6BAE-EDDC-458B-A50F-8902DF730CED}"; ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments) { CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true }; Process process = Process.Start(psi); process.WaitForExit(); return uninstallCommand; } } } return ""; } 

更新:使用WIX MSI安装程序后

我在WIX中创建了CustomAction项目,还使用WIX创建了安装项目。请参阅我的Product.wxs

   NOT Installed  

我有在CustomAction.cs.中卸载3个应用程序的代码。当我运行我的WIX MSI时,它安装新的应用程序并卸载第一个应用程序。剩下的两个应用程序都没有卸载,我注意到在成功卸载第一个应用程序之后的UI关闭,没有任何反应。

你能告诉我如何在安装我的WIX MSI时卸载3应用程序。

更新2:

         

product.wxs中的上述设置是卸载以前的版本并安装新版本。随之而来我还需要卸载另外两个依赖项应用程序。如何使用wix安装程序卸载依赖项应用程序。

任何人都可以帮助我检查机器中已安装的应用程序,并在安装新的wix msi之前将其卸载。

MSI中有一个互斥锁可以防止并发安装/卸载。 一切都必须在单个安装程序的上下文中发生。 也就是说,这样做的方法是将行编写到Upgrade表中以教授FindRelatedProducts和RemoveExistingProducts以删除其他已安装的MSI。

您没有提到您用于创建MSI的内容,因此我无法向您展示如何执行此操作。

您现在已经提到过您正在使用VDPROJ。 此工具不支持创作您要执行的操作。 我的建议是使用Windows Installer XML(WiX)重构并创作多个Upgrade元素以删除各种产品。

很抱歉有坏消息……但是:

从任何基于Windows的现代机器安装/卸载程序时 – 无法启动超过1个安装向导实例(简而言之:msiExec)

这就是为什么它在项目的其他部分工作得很好 – 因为在这些点上没有调用msiExec

现在好消息:你可以延迟发送unistall命令,甚至更好 – 启动一个定时器,如果安装结束,每隔X秒就会询问一次。 安装程序完成后,您将能够执行unistall命令。 像这样的东西:

  timer = new System.Timers.Timer(2 * 1000) { Enabled = true }; timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e) { } timer.Start(); // finally, call the timer 

可以使用Windows Management Instrumentation(WMI)卸载应用程序。 使用ManagementObjectSearcher获取需要卸载的应用程序,并使用ManagementObject Uninstall方法卸载应用程序。

 ManagementObjectSearcher mos = new ManagementObjectSearcher( "SELECT * FROM Win32_Product WHERE Name = '" + ProgramName + "'"); foreach (ManagementObject mo in mos.Get()) { try { if (mo["Name"].ToString() == ProgramName) { object hr = mo.InvokeMethod("Uninstall", null); return (bool)hr; } } catch (Exception ex) { } } 

以编程方式卸载应用程序(使用WMI)中给出的详细说明