none
Run Uninstaller before Installing in Visual Studio 2017 RRS feed

  • Question

  • We are creating a MSI installer with custom actions. When we install the MSI, we need to uninstall the application previously installed, for that we have created a function GetUninstallCommand(), and we are calling it inside the Install method. But it is not calling the function GetUninstallCommand().

    We have added Custom  Actions fro Install, Commint, RollBack, UnInstall

    When we call the function from the console application, it is uninstalling.

     Help me to solve it.
    public override void Install(System.Collections.IDictionary stateSaver)
    {
    
        GetUninstallCommand();
     
        try
        {
            base.Install(stateSaver);
            if (Context.Parameters[@"assemblypath"!= null)
            {
                RegisterInStartup(true, Context.Parameters[@"assemblypath"]);
            }
        }
        catch (Exception ex)
        {
            JsonHelper.HandelException(ex, "AppInstaller.Install");
        }
    }
    public static bool GetUninstallCommand()
    {
        try
        {
            string registryKey =@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
     
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey))
            {
                var query = from a in
                          key.GetSubKeyNames()
                            let r = key.OpenSubKey(a)
                            select new
                            {
                                Application = (string)r.GetValue("DisplayName"),
                                UninstallString = (string)r.GetValue("UninstallString")
                            };
     
                foreach (var item in query.Where(a => a.Application.ToString().ToLower() == "xxxx".ToLower()))
                {
                    if (item.Application == null)
                        continue;
     
                    if (item.Application.ToString().ToLower() == "XXXX".ToLower())
                    {
                        Console.WriteLine(item.Application + "+" + item.UninstallString);
     
                        return UnInstallApp(item.UninstallString);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            JsonHelper.HandelException(ex, "GetUninstallCommand");
            return false;
        }
     
        return true;
    }
     
    private static bool UnInstallApp(string productCode)
    {
        try
        {
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            process.StartInfo = startInfo;
     
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardError = true;
     
            startInfo.FileName = "MsiExec.exe";
            startInfo.Arguments = productCode.Replace("MsiExec.exe /I"" /X");
     
            process.Start();
     
            process.WaitForExit();
     
            return true;
        } catch(Exception ex)
        {
            return false;
        }
    }

    Thanks,

    Babu.



    • Edited by Babu R K Monday, January 6, 2020 2:30 PM
    Monday, January 6, 2020 2:27 PM

All replies

  • Hi Babu,

    Thanks for posting here.

    For the issue, I will discuss with relevant engineer and update the answer in the first time if there is a conclusion.

    Best regards,

    Strive


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 8, 2020 9:38 AM