使用C#执行卸载操作

3

我在互联网上寻找了这个问题,但是找不到答案。

有没有办法通过C#触发卸载程序(从“程序和功能”屏幕)?或者这被Windows为安全目的所阻止了?

3个回答

5
您可以使用msiexec.exe。您可以使用应用程序的产品代码简单卸载它。使用命令,您可以设置在卸载过程中是否显示UI或使其成为静默卸载,

string UninstallCommandString = "/x {0} /qn";

  • /qn: 设置用户界面级别:无
  • /qb: 设置用户界面级别:基本UI
  • /qr: 设置用户界面级别:减少UI
  • /qf: 设置用户界面级别:完整UI(默认)

C#代码

string UninstallCommandString = "/x {0} /qn";

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;

startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;

startInfo.FileName = "msiexec.exe";
startInfo.Arguments = string.Format(UninstallCommandString, "Product Code");

process.Start();

1
@GuidoVisser请确保提供您安装的正确产品代码。 - Kurubaran
1
如何查找程序的“产品代码”? - T.Todua


0
你可以使用system.diagnostics来调用卸载程序的可执行文件。
类似下面这样的代码应该能解决问题:
System.Diagnostics.Process.Start("/path/to/uninstall.exe", "arguments for uninstaller if needed, else don't bother with this arg");

这个方法快速而且简单,/应该/会起作用的。希望可以帮到你。

编辑- 我刚意识到你想从添加或删除软件屏幕上进行操作。不过我还是把这个留在这里吧,我的错。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接