如何在c#中以编程方式卸载软件。?
Microsoft.Win32.RegistryKey Fregistry = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(“SOFTWARE”)
.OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion")
.OpenSubKey("Installer").OpenSubKey("UserData")
.OpenSubKey("S-1-5-18").OpenSubKey("Products");
string[] Names = Fregistry.GetSubKeyNames();
string uninstall = "";
string ApplicationName = "Studio V5";
for (int i = 0; i < Names.Length; i++)
{
Microsoft.Win32.RegistryKey FTemp = Fregistry.OpenSubKey(Names[i]).OpenSubKey("InstallProperties");
**if (FTemp.GetValue("DisplayName").ToString() == ApplicationName)**
{
object obj = FTemp.GetValue("UninstallString");
if (obj == null)
uninstall = "";
else
uninstall = obj.ToString();
i = Names.Length;
}
}
System.Console.WriteLine(uninstall);
System.Diagnostics.Process FProcess = new System.Diagnostics.Process();
string temp = "/x{" + uninstall.Split("/".ToCharArray())[1].Split("I{".ToCharArray())[2];
//replacing with /x with /i would cause another popup of the application uninstall
FProcess.StartInfo.FileName = uninstall.Split("/".ToCharArray())[0];
FProcess.StartInfo.Arguments = temp;
FProcess.StartInfo.UseShellExecute = false;
FProcess.Start();
System.Console.Read();
我收到了一个错误,如**行中的NULL REFERENCE EXCEPTION。
答案 0 :(得分:2)
这是一种非常讨厌的方式,我怀疑它在任何情况下都能保证有效。相反,您应该使用Windows Installer API,特别是MsiConfigureProduct
和/或MsiConfigureFeature
函数与INSTALLSTATE_ABSENT
以编程方式卸载任何内容。