I have a WPF application which uses some image files stored locally in machine. As a cleanup process I have to delete all images when application closes.
For clearing i am using IDisposable and calling method to delete the Image files but method throws exception that file cannot be deleted as it is used by process.
On implementing destructor and calling cleanup method from it works fine and it cleans all files, but I am not allowed to use it.
Need help to get that particular location from where I can call cleanup.
just for reference, below code I am using for deleting images by implementing IDisposable.
private void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
this.CleanUp();
this.disposed = true;
}
}
}
void CleanUpModule(object sender, EventArgs e)
{
var folderPath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Somelocation\\IconImages\\");
if (Directory.Exists(folderPath) == false)
{
return;
}
// Clean all the temporary files.
foreach (var file in Directory.GetFiles(folderPath))
{
File.Delete(file);
}
}