重复项:欢迎来到 Stackoverflow。顺便提一下,我看到这个问题至少有 3 种不同的形式。我们将不得不关闭您的一些问题,因为重复会分散回复,并且如果人们回答(看似)未回答的重复问题,可能会浪费大量时间。
简而言之:请不要多次发布同一问题。以下是其他问题:
- MsiExec.exe 产品 ID 卸载 https://stackoverflow.com/questions/52271051/msiexec-exe-product-id-uninstall
- MSI 安装程序选项 - 卸载应用程序 https://stackoverflow.com/questions/52267512/msi-installer-option-uninstalling-an-application
C#:使用 C# 来完成此任务可能会很笨拙 - 无论您如何做。我不会推送命令行msiexec.exe
,但可以直接通过MSI API https://learn.microsoft.com/en-us/windows/desktop/msi/windows-installer-reference。该 API 可以通过以下方式访问Win32 函数 https://learn.microsoft.com/en-us/windows/desktop/msi/installer-function-reference or COM自动化 https://learn.microsoft.com/en-us/windows/desktop/msi/automation-interface.
MSI 的卸载方法:供您参考,有多种方法可以取消 MSI
卸载:从命令行卸载 MSI 文件而不使用 msiexec https://stackoverflow.com/questions/450027/uninstalling-an-msi-file-from-the-command-line-without-using-msiexec/1055933#1055933.
第 14 条上面的链接显示了如何使用 C++ 卸载 - 如果可以选择的话。但是:Visual Studio 2017 模板再次发生了变化,因此可能需要进行调整才能“开箱即用”。
但是,我会使用 MSI API - 如前所述 - 我建议您通过本机 Win32 函数并使用DTF http://forum.installsite.net/index.php?showtopic=18128(部署工具基金会)是 WiX 工具包的一部分。它是 MSI API 的 .NET 包装器 - 这将为您节省大量样板代码,但代价是必须部署 DTF DLL:Microsoft.Deployment.WindowsInstaller.dll
与您的产品一起。我不知道这是否可以接受。如果需要的话,我有不依赖于 DTF 的代码,但它要长得多。
模型 C# 示例。项目参考Microsoft.Deployment.WindowsInstaller.dll
需要。然后在新的 C# .NET 项目中尝试以下代码。您可以通过安装来获取该DLLWiX 工具包 http://wixtoolset.org/releases/- 用于创建 MSI 文件的开源工具包。安装后签入%ProgramFiles(x86)%\WiX Toolset v3.11\bin
(针对 WiX 版本进行调整 - 截至 2018 年 9 月)。
安装程序图形用户界面:首先重要说明:设置的UI level是通过设置Installer.SetInternalUI
功能。如果以静默模式运行,则需要运行提升的可执行文件才能正常卸载,否则会出现访问异常。当您在完整 GUI 模式下运行时,您需要自行提升安装 - 前提是您有权这样做。
运行高架:如何检查管理员权限:检查当前用户是否为管理员 https://stackoverflow.com/questions/3600322/check-if-the-current-user-is-administrator/3600338#3600338.
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;
var productname = productname.ToLower();
foreach (ProductInstallation product in ProductInstallation.AllProducts)
{
if (product.ProductName.ToLower().Contains(productname))
{
productcode = product.ProductCode;
break;
}
}
return productcode;
}
}
}