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

时间:2014-11-11 11:00:37

标签: c# visual-studio-2010 c#-4.0 wix

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

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

卸载方法:

installer.cs

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

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

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 "";
        }

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

您能告诉我如何在安装WIX MSI期间卸载3应用程序。

更新2:

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

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

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

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:-1)

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

从任何基于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

答案 2 :(得分:-3)

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

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)
        {

        }
    }

Uninstalling applications programmatically (with WMI)

中给出的详细解释
相关问题