Uninstall the existing WPF application installed on windows when reinstalling

The project I’m currently working on is a could base .net core react web application, a web version of the desktop application the customer has now. We have 3 setups (MSI installer) written using WPF. In order to enable the web version we have to activate it first on the customer’s windows machines.

Customers can activate and connect with the web version first time. and then we have updates to be installed from time to time using the same setup. once user runs the installer setup. it will install all 2 other setups and will open the last setup, let’s say it as connector.

The issue was the connector setup didn’t open automatically when install updates. so I had to uninstall it from windows by the code before install updates.

This is how I achieved it.

  • First, we should know the name of the app, you can find it easily from the setup project (in my case we have a WIX project).
  • Below are the methods we have to implement and it will uninstall the application from windows add/remove programmes.
  • Please note that when we retrieve the Uninstall string, I have considered both windows 32bit and 64bit versions in the registry paths.
  • The Uninstall string would looks like this – “msiexec.exe /x 0C2B6785508321B44A7EB21A23EFB117”
    private void UninstallConnectorSetup()
    {

      var uninstallString = GetUninstallString("<app name>");
      Process process = new Process();
      process.StartInfo.UseShellExecute = false;
      process.StartInfo.RedirectStandardError = true;
      process.StartInfo.FileName = "msiexec.exe";
      process.StartInfo.Arguments = uninstallString;
      process.Start();
    }

    private string GetUninstallString(string msiName)
    {
      string uninstallString = string.Empty;
      try
      {
        string path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products";
        RegistryKey key = Registry.LocalMachine.OpenSubKey(path);
        if (key == null)
        {
          key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).
          OpenSubKey(path);
        }
        foreach (string tempKeyName in key.GetSubKeyNames())
        {
          RegistryKey tempKey = key.OpenSubKey(tempKeyName + "\\InstallProperties");
          if (tempKey != null)
          {
            if (string.Equals(Convert.ToString(tempKey.GetValue("DisplayName")), msiName, StringComparison
            .CurrentCultureIgnoreCase))
            {
              uninstallString = Convert.ToString(tempKey.GetValue("UninstallString"));
              uninstallString = uninstallString.Replace("/I", "/X");
              uninstallString = uninstallString.Replace("MsiExec.exe", "").Trim();
              uninstallString += " /quiet /qn";
              break;
            }
          }
        }
        return uninstallString;
      }

      catch (Exception ex)
      {
        throw new ApplicationException(ex.Message);
      }
    }

Leave a comment