问题描述
我尝试了这个,this 以编程方式卸载应用程序.我没有收到任何错误或异常,但应用程序没有从我的机器上卸载.另请参阅尝试过的代码
I tried this,this to uninstall the application programmatically. I am not getting any error or exception but the application is not uninstalled from my machine. Please see tried code also
public static string GetUninstallCommandFor(string productDisplayName)
{
RegistryKey localMachine = Registry.LocalMachine;
string productsRoot = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products";
RegistryKey products = localMachine.OpenSubKey(productsRoot);
string[] productFolders = products.GetSubKeyNames();
foreach (string p in productFolders)
{
RegistryKey installProperties = products.OpenSubKey(p + @"\InstallProperties");
if (installProperties != null)
{
string displayName = (string)installProperties.GetValue("DisplayName");
if ((displayName != null) && (displayName.Contains(productDisplayName)))
{
string uninstallCommand =(string)installProperties.GetValue("UninstallString");
return uninstallCommand;
}
}
}
return "";
}
请帮助我使用 C# 以编程方式卸载应用程序.
Please help me to uninstall the application programmatically using C#.
推荐答案
上面的例程将返回一个字符串,假设它找到了一个可能如下所示的匹配项:
The above routine will return a string, assuming it found a match that may look like:
MsiExec.exe/X{02DA0248-DB55-44A7-8DC6-DBA573AEEA94}
你需要把它当作一个进程来运行:
You need to take that and run it as a process:
System.Diagnostics.Process.Start(uninstallString);
注意它可能并不总是 msiexec,它可以是程序选择指定的任何内容.在 msiexec 的情况下,您可以将 /q
参数附加到您的 uninstallString
以使其静默卸载(并且不会显示那些修复/删除对话框).
Note that it may not be always msiexec, it can be anything that the program chooses to specify. In case of msiexec, you can append /q
parameter to your uninstallString
to make it uninstall silently (and it won't show those Repair/Remove dialogs).
更新:如果您使用的是 Windows 安装程序 3.0 或更高版本,您还可以使用 /quiet
进行静默安装/卸载.它与 /qn
基本相同(如果您使用的是旧版本).来源.感谢@JRO 提出它!
Update: If you're using Windows installer 3.0 or above, you can also use /quiet
for silent install/uninstall. It's basically same as /qn
(if you're on older versions). Source. Thanks @JRO for bringing it up!
这篇关于如何以编程方式卸载应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!