5

I am creating a C# .NET WinForms application and I am creating the installer as a Visual Studio setup project.

On Windows 10, I can remove the installed files in the control panel. However, during runtime my application creates a folder containing log files, and this folder and the log files are not removed when the app is uninstalled.

How can I make these files also be removed when the program is uninstalled?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
shadowsora
  • 663
  • 7
  • 15

1 Answers1

14

You can use a custom installer action to perform custom actions during installation or uninstalling the application. To do so, you need to add a new class library containing a class which derives from CustomAction.

To do so, follow these steps:

  1. Add a new Setup project. (If you don't have project template, download and install it from here for VS20 13, VS 2015, VS 2017 and VS 2019, VS 2022)
  2. Add primary output from your main project to the setup project.
  3. Add a new class library project.
  4. Add a new installer action to the class library project and use the code at the end of these steps.
  5. Add primary output of the class library to the setup project
  6. Right click on setup project in solution explorer and in view menu, select Custom Actions.
  7. In the custom actin editor, right click on uninstall and select Add Custom Action ... and select primary output of class library.
  8. Rename the action to RemoveFiles and in properties set CustomActionData property exactly to /path="[TARGETDIR]\".
  9. Rebuild the solution and the setup project.
  10. Install the project.

Code for Custom Action

Add a reference to System.Configuration.Install assembly and then add a class to the project having following content. You can simply have any logic that you need here.

using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

namespace InstallerActions
{
    [RunInstaller(true)]
    public partial class RemoveFiles : Installer
    {
        protected override void OnAfterUninstall(IDictionary savedState)
        {
            var path = System.IO.Path.Combine(Context.Parameters["path"], "log");
            System.IO.Directory.Delete(path, true);
        }
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    Exactly what I was looking for! Just wondering, instead of using s specific target directory, is there anyway I can grab the install directory? AppDomain.CurrentDomain.BaseDirectory (what I use in the main app) did not achieve this – shadowsora Oct 17 '17 at 13:16
  • 3
    `Context.Parameters["path"]` is the install directory. – Reza Aghaei Oct 17 '17 at 13:54
  • This post was really helpful. In my case I want to delete the files after uninstalling the application. so I have written installer code on OnAfterUninstall but it deletes the files I wanted and then uninstall the app. Strange. Any suggestions ? – Krrish Jul 01 '20 at 15:24
  • How do you delete directories outside the install directory? – Zimo Nov 16 '21 at 19:12
  • @Zimo, if you have created them, you should have the paths somewhere like a config file or somewhere else, then you can remove them. The other option is you probably know their path relative to the install folder and then you can easily combine the relative path with the install folder path and then remove them. – Reza Aghaei Nov 17 '21 at 12:56
  • Does anyone know if it works with .NET 6? – Damian Ubowski Feb 02 '23 at 07:28
  • 1
    @DamianUbowski The Install class is [not available in .NET 6](https://learn.microsoft.com/en-us/dotnet/api/system.configuration.install.installer?view=netframework-4.8.1#applies-to), but keep in mind, it's not expected to be in the assembly of your application, so you can easily create your app with .NET 6, and the assembly of this installer with .NET 4.8. – Reza Aghaei Feb 02 '23 at 08:29
  • @DamianUbowski And for VS 2022, use this extension: [Microsoft Visual Studio Installer Projects 2022](https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2022InstallerProjects) – Reza Aghaei Feb 02 '23 at 08:33