简体   繁体   中英

Trying to uninstall an application but getting an error

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"

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

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