在安装过程中卸载应用程序不起作用



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

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

卸载方法:

public static string GetUninstallCommandFor(string productDisplayName)
        {
            RegistryKey localMachine = Registry.LocalMachine;
            string productsRoot = @"SOFTWAREMicrosoftWindowsCurrentVersionInstallerUserDataS-1-5-18Products";
            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创建了Setup项目。请参阅我的Product.wxs

 <InstallExecuteSequence>
      <Custom Action='ShowCustomActionCustomAction' After='InstallFinalize'>NOT Installed</Custom>
    </InstallExecuteSequence>

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

你能告诉我如何在安装我的WIXMSI期间卸载3应用程序吗。

更新2:

<Property Id="PREVIOUSVERSIONSINSTALLED" Secure="yes" />
    <Upgrade Id="89CF8BE7-05EE-4C7E-9EFC-0249DD260EBB">
      <UpgradeVersion
         Minimum="1.0.0.0" Maximum="99.0.0.0"
         Property="PREVIOUSVERSIONSINSTALLED"
         IncludeMinimum="yes" IncludeMaximum="no" />
    </Upgrade>
<InstallExecuteSequence>     
      <RemoveExistingProducts Before="InstallFinalize" />
    </InstallExecuteSequence>
  </Product>

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

有人能帮助我如何在安装我的新wix-msi之前检查机器中安装的应用程序并将其卸载吗。

MSI中有一个互斥体,它阻止并发安装/卸载。一切都必须在单个安装程序的上下文中进行。也就是说,这样做的方法是将行写入Upgrade表中,以教导FindRelatedProducts和RemoveExistingProducts删除额外安装的MSIs。

你没有提到你正在使用什么来创建你的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获取需要卸载的应用程序,并使用ManagementObjectUninstall方法卸载该应用程序。

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)卸载应用程序中给出的详细解释

相关内容

最新更新