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?
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:
CustomActionData
property exactly to /path="[TARGETDIR]\"
.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);
}
}
}