1

At work I have to uninstall the same application several times per day (and reinstall new versions). I'm trying to make a C# program that I can run that will do this all for me. Right now, I'm stuck on the uninstall process.

The code I have runs and attempts to uninstall the application, but Windows gives me an error during this.

Here's what I have right now:

static void Main(string[] args)
    {
      ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
      const string prodName = "myApp";
      Console.WriteLine("searching...");
      foreach (ManagementObject wmi in searcher.Get())
      {
        if (wmi["Name"].ToString() == prodName)
        {
          Console.WriteLine("found");
          string productCode = "/x {" + wmi.Properties["IdentifyingNumber"].Value.ToString() + "}";
          Process p = new Process();
          p.StartInfo.FileName = "msiexec.exe";
          p.StartInfo.Arguments = productCode;
          p.Start();
          Console.WriteLine("Uninstalled");
          break;
        }
      }
      Console.ReadLine();
    }

This is the error I get when it tries to do the uninstall:

"This installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package"

tethys4
  • 55
  • 1
  • 11

1 Answers1

0

Uninstall Reference: This old answer might have what you need: Uninstalling an MSI file from the command line without using msiexec. It shows various ways to uninstall an MSI.

DTF: The DTF (Desktop Tools Foundation) included with the WiX toolset can be used to uninstall. Here is some sample C# code: Uninstalling program - run elevated, quick link to sample.

VBScript / COM: Here are some links to VBScript samples and various ways to uninstall (uninstall by product name, uninstall by upgrade code, etc...): WIX (remove all previous versions) - the uninstall by upgrade code (sample code towards bottom) is interesting because it generally works for all versions and incarnations of your installer (upgrade code tends to remain stable across releases).

CMD.EXE: Personally I might just use an install and an uninstall batch file. More of these samples in section 3 here. Just create two different files:

  • Install: msiexec.exe /i "c:\Setup.msi" /QN /L*V "C:\msi.log" REBOOT=ReallySuppress

  • Uninstall: msiexec.exe /x "c:\Setup.msi" /QN /L*V "C:\msi.log" REBOOT=ReallySuppress

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
  • It's also worth using Process Monitor to see which installer in the Windows Installer cache it is trying to uninstall, and whether or the not the context you are running your program from has the required permissions. Look for "Access Denied" etc. – Captain_Planet Sep 11 '19 at 07:20
  • 1
    Good point, added link to code to check for admin rights in C#. – Stein Åsmul Sep 11 '19 at 10:09