0

How to uninstall the software programmatically in 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();

I got an error like....NULL REFERENCE EXCEPTION in the ** line.

0

1 Answer 1

2

This is a very nasty way to do this, and I doubt it is guaranteed to work in any case. Instead, you should use the Windows Installer API, and specifically MsiConfigureProduct and/or MsiConfigureFeature functions with INSTALLSTATE_ABSENT to programmatically uninstall anything.

Sign up to request clarification or add additional context in comments.

2 Comments

Does this work with software that has been not installed using Windows Installer?
Windows Installer in this case is an API, not any particular installer format (such as .msi). A non-.msi installer still can (and should) use WI APIs to register all components that it installs. If it does not do that, then it would probably not write the registry key under Microsoft\Installer either, and isn't discoverable anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.