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:
- 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)
- Add primary output from your main project to the setup project.
- Add a new class library project.
- Add a new installer action to the class library project and use the code at the end of these steps.
- Add primary output of the class library to the setup project
- Right click on setup project in solution explorer and in view menu, select Custom Actions.
- In the custom actin editor, right click on uninstall and select Add Custom Action ... and select primary output of class library.
- Rename the action to RemoveFiles and in properties set CustomActionDataproperty exactly to/path="[TARGETDIR]\".
- Rebuild the solution and the setup project.
- 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);
        }
    }
}