4

I am developing WPF application in C#. Currently my msi install the current application in machine.I need to uninstall two supporting application of existing version during the installation of new one(msi).

I written code to uninstall application programmatically and this does not work when i call the application uninstallation method in installer.cs.The same method uninstall the two application successfully when i call from other part of project other than installer.cs.

uninstallation method:

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 fileName = "MsiExec.exe";
                        string arguments = "/x{4F6C6BAE-EDDC-458B-A50F-8902DF730CED}";

                        ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments)
                        {
                            CreateNoWindow = true,
                            UseShellExecute = false,
                            RedirectStandardOutput = true
                        };

                        Process process = Process.Start(psi);

                        process.WaitForExit();

                        return uninstallCommand;
                    }
                }
            }

            return "";
        }

Update:After using WIX MSI Installer

I have created the CustomAction project in WIX, also created the Setup project using WIX.Please see my Product.wxs

 <InstallExecuteSequence>
      <Custom Action='ShowCustomActionCustomAction' After='InstallFinalize'>NOT Installed</Custom>
    </InstallExecuteSequence>

I have the code to uninstall 3 application in CustomAction.cs.When i run my WIX MSI,it install the new application and uninstall the first application.The remaining two application are not uninstalled,i noticed that after successful uninstall of first application the UI closes and nothing happens.

Can you tell me how to uninstall the 3 application during the installation of my WIX MSI.

Update 2:

<Property Id="PREVIOUSVERSIONSINSTALLED" Secure="yes" />
    <Upgrade Id="89CF8BE7-05EE-4C7E-9EFC-0249DD260EBB">
      <UpgradeVersion
         Minimum="1.0.0.0" Maximum="99.0.0.0"
         Property="PREVIOUSVERSIONSINSTALLED"
         IncludeMinimum="yes" IncludeMaximum="no" />
    </Upgrade>
<InstallExecuteSequence>     
      <RemoveExistingProducts Before="InstallFinalize" />
    </InstallExecuteSequence>
  </Product>

the above settings in product.wxs is uninstalling the previous version and install the new one.Along with this i need to uninstall another two dependency application also.How to uninstall the dependency application using wix installer.

Can any one help me how to check the installed application in the machine and uninstall that before installation of my new wix msi.

user2725407
  • 400
  • 1
  • 4
  • 16

3 Answers3

3

There is a mutex in MSI that prevents concurrent installation / uninstalls. Everything has to happen within the context of a single installer. That said, the way to do that is to author rows into the Upgrade table to teach FindRelatedProducts and RemoveExistingProducts to remove the additional installed MSIs.

You don't mention what you are using to create your MSI so I can't show you how to do that.

You've now mentioned that you are using VDPROJ. This tool doesn't support authoring what you are trying to do. My suggestion is to refactor using Windows Installer XML (WiX) and author multiple Upgrade elements to remove the various products.

Christopher Painter
  • 54,556
  • 6
  • 63
  • 100
  • I am using VS2010 to create MSI and i can see the MSI table using ORCA.Could you please show me how to do. – user2725407 Nov 11 '14 at 14:01
  • Yet another reason to not use Visual Studio Deployment Projects. Post build hacking the MSI would be like tweaking the IL of a DLL because C# couldn't author something. Who in their right mind would do that? – Christopher Painter Nov 11 '14 at 15:00
  • @ChristopherPainter I have tried as per your suggestion.Now while installing,it installs and uninstall one application.I want to remove totally 3 application during the installation of my wix msi.I have updated my question.Please see my Update in question. – user2725407 Nov 13 '14 at 16:39
  • @ChristopherPainter I have updated the question.Can you please guide me how to uninstall more than one application during installation using wix msi. – user2725407 Nov 14 '14 at 17:17
  • You don't need the custom action. Just author Upgrade elements for each of the MSIs you want removed. – Christopher Painter Nov 14 '14 at 18:16
  • @ChristopherPainter I am not much families with using wix,can you show me a sample how to do author Upgrade elements for each MSI. – user2725407 Nov 14 '14 at 18:38
  • @ChristopherPainter Can you guide me how to install the application in C: partition instead of Program Files folder.The folder structure like this C:\Applicationfolder,need to install the application inside the Applicationfolder. – user2725407 Nov 18 '14 at 13:42
  • You should ask that as a new question. – Christopher Painter Nov 18 '14 at 13:58
  • @ChristopherPainter I am using CA to uninstall my per user application,with wix it is working perfectly all right.When i bundled my msi and .net framework in bundle.wxs,at the time of CA to uninstall calls,the main application installation fails and i am getting the error as installation failed.Please have a look at this SO question http://stackoverflow.com/questions/27151665/error-0x80070643-fatal-error-during-installation-wix and guide me how to resolve this. – user2725407 Nov 27 '14 at 15:07
-1

sorry to be the one with the bad news... but:

when installing / uninstalling a program from any modern windows-based machine - there is no way to start more then 1 instance of the installation wizard (in short: the msiExec)

that is why it works great on other parts of your project - because no calls for the msiExec are made at those points

and now for the good news: you can send the unistall command with a delay, or even better - start a timer that asks every X seconds if the installation is over. when the installer will complete, you will be able to make the unistall commands. something like this:

        timer = new System.Timers.Timer(2 * 1000) { Enabled = true };

        timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
        {

        }

        timer.Start(); // finally, call the timer
ymz
  • 6,602
  • 1
  • 20
  • 39
  • Not needed and a complete hack. A bootstrapper/chainer provides this functionality. – Christopher Painter Nov 22 '14 at 12:51
  • @ymz I am facing the same problem while using bootstapper.Can you please look into this SO question http://stackoverflow.com/questions/27151665/error-0x80070643-fatal-error-during-installation-wix?noredirect=1#comment42811701_27151665 and help me. – user2725407 Nov 27 '14 at 07:30
  • I tried your answer but after installation the method inside the timer.Elapsed is not called.Any idea about this. – user2725407 Nov 27 '14 at 09:01
  • @ymz I tried with timer,this timer is not calling in CA.I tried with CA by Before InstallFinalize,Before InstallInitialize.What i am placing inside the timer is not called.Other part is called by the installer.Any way to debug this or solve this. – user2725407 Nov 27 '14 at 15:02
-3

There is a option to uninstall the application using Windows Management Instrumentation (WMI). Using the ManagementObjectSearcher get the application needs to be Uninstalled and use the ManagementObject Uninstall method to uninstall the application.

ManagementObjectSearcher mos = new ManagementObjectSearcher(
      "SELECT * FROM Win32_Product WHERE Name = '" + ProgramName + "'");
    foreach (ManagementObject mo in mos.Get())
    {
        try
        {
            if (mo["Name"].ToString() == ProgramName)
            {
                object hr = mo.InvokeMethod("Uninstall", null);
                return (bool)hr;
            }
        }
        catch (Exception ex)
        {

        }
    }

Detailed explanation given in Uninstalling applications programmatically (with WMI)

Aravindhan
  • 107
  • 1
  • 6