卸载程序

我正在尝试使用此代码卸载程序..但它似乎不起作用。 我已经尝试了其他答案,但似乎也没有工作..有人可以帮助我吗? 我正在尝试按给定名称卸载程序(displayName)

例如,我给displayName = Appname,然后这段代码应该从我的计算机上卸载Appname程序。

public static void UninstallApplictionInstalled(string p_name) { string displayName; string uninstlString; RegistryKey key; ProcessStartInfo info = new ProcessStartInfo(); Process uninstallProcess = new Process(); string temp; // search in: CurrentUser key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"); foreach (String keyName in key.GetSubKeyNames()) { RegistryKey subkey = key.OpenSubKey(keyName); displayName = Convert.ToString(subkey.GetValue("DisplayName")); uninstlString = Convert.ToString(subkey.GetValue("UninstallString")); if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true) { uninstallProcess.StartInfo.FileName = "MsiExec.exe"; uninstallProcess.StartInfo.Arguments = " /x " + uninstlString + " /quiet /norestart"; uninstallProcess.Start(); uninstallProcess.WaitForExit(); break; //Console.WriteLine(subkey.GetValue("UninstallString")); } } // search in: LocalMachine_32 key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"); foreach (String keyName in key.GetSubKeyNames()) { RegistryKey subkey = key.OpenSubKey(keyName); displayName = Convert.ToString(subkey.GetValue("DisplayName")); uninstlString = Convert.ToString(subkey.GetValue("UninstallString")); if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true) { uninstallProcess.StartInfo.FileName = "MsiExec.exe"; uninstallProcess.StartInfo.Arguments = " /x " + uninstlString + " /quiet /norestart"; uninstallProcess.Start(); uninstallProcess.WaitForExit(); break; //Console.WriteLine(subkey.GetValue("UninstallString")); } } // search in: LocalMachine_64 key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"); foreach (String keyName in key.GetSubKeyNames()) { RegistryKey subkey = key.OpenSubKey(keyName); displayName = Convert.ToString(subkey.GetValue("DisplayName")); uninstlString = Convert.ToString(subkey.GetValue("UninstallString")); if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true) { //string prdctId = uninstlString.Substring((uninstlString.IndexOf("{"))); uninstallProcess.StartInfo.FileName = "MsiExec.exe"; uninstallProcess.StartInfo.Arguments = " /x " + uninstlString + " /quiet /norestart"; uninstallProcess.Start(); uninstallProcess.WaitForExit(); break; //Console.WriteLine(subkey.GetValue("UninstallString")); } } } 

只有这个弹出.. 在此处输入图像描述

重复 :欢迎使用Stackoverflow。 只是提到你,我看到这个问题至少有3种不同的口味。 我们将不得不关闭您的一些问题,因为复制会散布回复,并且如果人们回答(看似)未答复的重复项,可能会浪费大量时间。

简而言之:请不要多次发布同一个问题。 以下是其他问题:

  • MsiExec.exe产品ID卸载
  • MSI安装程序选项 – 卸载应用程序

C# :使用C#这可能很笨 – 无论你怎么做。 我不会将命令行推送到msiexec.exe ,而是直接通过MSI API 。 可以通过Win32函数COM自动化访问此API。

卸载适用于MSI的Appraches :供您参考,有多种方法可以启动MSI卸载: 从命令行卸载MSI文件而不使用msiexec 。

上面链接的第14节显示了如何使用C ++卸载 – 如果这是一个选项。 但是:,Visual Studio 2017模板再次发生了变化,因此可能需要进行调整才能“开箱即用”。

但是,我会使用MSI API – 如前所述 – 我建议您使用本机Win32函数,并使用DTF (部署工具基础),这是WiX工具包的一部分。 它是MSI API的.NET包装器 – 它将为您节省大量的样板代码,代价是必须部署DTF DLL: Microsoft.Deployment.WindowsInstaller.dll以及您的产品。 我不知道这是否可以接受。 如果需要,我的代码不依赖于DTF,但它要长得多。

模拟C#样本 。 需要项目引用Microsoft.Deployment.WindowsInstaller.dll 。 然后在新的C#.NET项目中尝试以下代码。 您可以通过安装WiX工具包(用于创建MSI文件的开源工具包)来获取该DLL。 安装后检查%ProgramFiles(x86)%\WiX Toolset v3.11\bin (调整WiX版本 – 截至2018年9月的当前版本)。

安装程序GUI :重要说明:安装程序的UI级别是通过Installer.SetInternalUI函数设置的。 如果以静默方式运行,则需要运行提升的可执行文件以使卸载正常工作,否则将发生访问exception。 在完全GUI模式下运行时,您需要自己提升安装 – 前提是您有权这样做。

 using System; using Microsoft.Deployment.WindowsInstaller; namespace UninstallMsiViaDTF { class Program { static void Main(string[] args) { // Update this name to search for your product. This sample searches for "Orca" var productcode = FindProductCode("orca"); try { if (String.IsNullOrEmpty(productcode)) { throw new ArgumentNullException("productcode"); } // Note: Setting InstallUIOptions to silent will fail uninstall if uninstall requires elevation since UAC prompt then does not show up Installer.SetInternalUI(InstallUIOptions.Full); // Set MSI GUI level (run this function elevated for silent mode) Installer.ConfigureProduct(productcode, 0, InstallState.Absent, "REBOOT=\"ReallySuppress\""); // Check: Installer.RebootInitiated and Installer.RebootRequired; } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } Console.ReadLine(); // Keep console open } // Find product code for product name. First match found wins static string FindProductCode(string productname) { var productcode = String.Empty; foreach (ProductInstallation product in ProductInstallation.AllProducts) { if (product.ProductName.ToLower().Contains(productname.ToLower())) { productcode = product.ProductCode; break; } } return productcode; } } }