1

I am using C# and .NET 4.0 framework to develop a WPF application. I need to perform a task like this: Before installing my WPF application on the desktop, I need to remove some folder in that installing desktop. I tried the below one in:

File installer.cs

this.BeforeInstall +=new InstallEventHandler(Installer_BeforeInstall);

void Installer_BeforeInstall(object sender, InstallEventArgs e)
{
    // Code
}

Whatever code I used in the above methods to remove the folder/directory are executed after my WPF application installed, means in half of the installation. I need to remove the folder before the WPF application saves the installation folder on my desktop. How do I do this in C# using .NET 4.0?

Update: installer.cs

namespace namespace name
{
    [RunInstaller(true)]
    public partial class Installer : System.Configuration.Install.Installer
    {
        public Installer()
        {
            InitializeComponent();

            this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);

            this.BeforeUninstall += new InstallEventHandler(ServiceInstaller_BeforeUninstall);
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
        }

        void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            //code
        }

        void ServiceInstaller_BeforeUninstall(object sender, InstallEventArgs e)
        {
            // Code
        }
    }
}
Community
  • 1
  • 1
user2681579
  • 1,413
  • 2
  • 23
  • 50
  • 3
    As a suggestion: Use a framework like WIX (free) to make your installer. Installing something is a complicated task, especially if something unexpected happens. WiX needs some time to understand, but is very powerful – Christian Sauer Nov 03 '14 at 18:38
  • What are you using to install the application? InstallShield, Wix, ClickOnce, xcopy, ... ?? – Mrchief Nov 03 '14 at 18:54
  • @Mrchief I am using VS2010 to build the solution.MSI file is generated and i am using that msi file to instal. – user2681579 Nov 03 '14 at 19:01
  • Any chance you can post in the full `installer.cs` code? – Mrchief Nov 03 '14 at 19:15
  • I think your best bet is to use a pure msi solution. there is a removefile table. Use orca to do this see http://www.symantec.com/connect/forums/how-remove-folder-uninstall-was-not-created-install – Joe Nov 10 '14 at 15:18
  • @Joe I want to uninstall a application on installation of my msi file and remove some unwanted folder from directory.Can you tell me how to do this. – user2681579 Nov 11 '14 at 06:43
  • that link has a vbscript file that you should be able to adapt. – Joe Nov 11 '14 at 14:46

1 Answers1

4

Deleting files/folder on target systems is dangerous and should be avoided. You should only delete files that you create; you being your MSI file here. Now that you have been warned, here are your options:

  • Using a Visual Studio Setup project, this is not possible, or at least not trivial. You need to implement a custom action using C++, compile into a DLL, build you MSI, change the install sequence using Orca to run before CostFinalize.

  • Use a third-party tool to give more power to installer projects, such as AdvancedInstaller

  • If you do decide to go third party, I would say use WiX. Look at RemoveFolder action or you can use a CustomAction like this:

    <CustomAction Id="DeleteFiles" Return="check" Value="del path\to\folder" />
    
    <InstallExecuteSequence>
      <Custom Action="DeleteFiles" After='InstallFinalize'>Installed AND NOT UPGRADINGPRODUCTCODE</Custom>
      ...
    </InstallExecuteSequence>
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • I want to uninstall an existing application on installation of my msi file.How to do this using Wix tool. – user2681579 Nov 11 '14 at 06:33
  • 1
    Take a look at http://stackoverflow.com/questions/6483669/wix-uninstall-different-product and http://stackoverflow.com/questions/19025798/wix-uninstall-product-using-command-line-during-installation – Mrchief Nov 12 '14 at 19:41