尝试卸载应用程序但出现错误

时间:2019-09-10 22:52:52

标签: c# windows windows-installer

在工作中,我每天必须多次卸载同一应用程序(并重新安装新版本)。我正在尝试制作一个可以运行的C#程序,为我完成所有这些工作。现在,我陷入了卸载过程。

我运行的代码试图卸载应用程序,但是Windows在此过程中给我一个错误。

这就是我现在拥有的:

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();
    }

这是我尝试执行卸载时遇到的错误:

“无法打开此安装软件包。请验证该软件包存在并且可以访问它,或与应用程序供应商联系以确认这是有效的Windows Installer软件包”

1 个答案:

答案 0 :(得分:0)

  

卸载参考 :这个旧答案可能满足您的要求:Uninstalling an MSI file from the command line without using msiexec。它显示了多种卸载MSI的方法。

     

DTF WiX toolset附带的DTF(台式机工具基金会)可用于卸载。这是一些示例C#代码:    Uninstalling program   -run elevated, quick link to sample

VBScript / COM :以下是一些指向VBScript示例的链接以及各种卸载方式( uninstall by product name uninstall by upgrade code etc... ):WIX (remove all previous versions)-uninstall by upgrade code(朝下的示例代码)很有趣,因为它通常适用于所有安装程序的版本和版本(升级代码在各个发行版中往往保持稳定)。

CMD.EXE :就我个人而言,我可能只使用install和一个卸载批处理文件。 More of these samples in section 3 here。只需创建两个不同的文件:

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

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

相关问题