Is there any way to delete a folder in run time if the memory of any file inside the folder is being used

43 Views Asked by At
if (Directory.Exists("BarCodePNG"))
{
    Directory.Delete("BarCodePNG", true);
}

if (Directory.Exists("BarCodeJPG"))
{
    Directory.Delete("BarCodeJPG", true);
}

if (Directory.Exists("BarCodeJoinJPG"))
{
    Directory.Delete("BarCodeJoinJPG", true);
}

if (Directory.Exists("output.pdf"))
{
    Directory.Delete("output.pdf", true);
}

The error is

System.IO.IOException: 'The process cannot access the file '2072675160.png' because it is being used by another process.'

Where as I want the folder to be force deleted

1

There are 1 best solutions below

0
Aminmoharrami On

//I think you need to close the running data before executing the delete code

string location = new Uri(window.LocationURL).LocalPath;
if (string.Equals(location, @"C:\close\me", 
StringComparison.OrdinalIgnoreCase))
            window.Quit();