简体   繁体   中英

Uninstall a application during installation not working

I am developing WPF application in C#. Currently my msi install the current application in machine.I need to uninstall two supporting application of existing version during the installation of new one(msi).

I written code to uninstall application programmatically and this does not work when i call the application uninstallation method in installer.cs .The same method uninstall the two application successfully when i call from other part of project other than installer.cs .

uninstallation method:

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

Update:After using WIX MSI Installer

I have created the CustomAction project in WIX, also created the Setup project using WIX.Please see my Product.wxs

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

I have the code to uninstall 3 application in CustomAction.cs.When i run my WIX MSI,it install the new application and uninstall the first application.The remaining two application are not uninstalled,i noticed that after successful uninstall of first application the UI closes and nothing happens.

Can you tell me how to uninstall the 3 application during the installation of my WIX MSI.

Update 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>

the above settings in product.wxs is uninstalling the previous version and install the new one.Along with this i need to uninstall another two dependency application also.How to uninstall the dependency application using wix installer.

Can any one help me how to check the installed application in the machine and uninstall that before installation of my new wix msi.

There is a mutex in MSI that prevents concurrent installation / uninstalls. Everything has to happen within the context of a single installer. That said, the way to do that is to author rows into the Upgrade table to teach FindRelatedProducts and RemoveExistingProducts to remove the additional installed MSIs.

You don't mention what you are using to create your MSI so I can't show you how to do that.

You've now mentioned that you are using VDPROJ. This tool doesn't support authoring what you are trying to do. My suggestion is to refactor using Windows Installer XML (WiX) and author multiple Upgrade elements to remove the various products.

sorry to be the one with the bad news... but:

when installing / uninstalling a program from any modern windows-based machine - there is no way to start more then 1 instance of the installation wizard (in short: the msiExec)

that is why it works great on other parts of your project - because no calls for the msiExec are made at those points

and now for the good news: you can send the unistall command with a delay, or even better - start a timer that asks every X seconds if the installation is over. when the installer will complete, you will be able to make the unistall commands. something like this:

        timer = new System.Timers.Timer(2 * 1000) { Enabled = true };

        timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
        {

        }

        timer.Start(); // finally, call the timer

There is a option to uninstall the application using Windows Management Instrumentation (WMI). Using the ManagementObjectSearcher get the application needs to be Uninstalled and use the ManagementObject Uninstall method to uninstall the application.

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

        }
    }

Detailed explanation given in Uninstalling applications programmatically (with WMI)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM