I have used this code in many projects for cleaning up temp files before closing the application, in my current project I see problems. It is not deleting all the files every time. Approx 20-50 files and sometimes 8-12 are left undeleted. If I run the same code twice, it will delete the rest of the files. The files are not in use and I have full access.
What is going on and how can I fix this issue?
OS: Windows 10, Framework: .NET 4.5
Edit / update: - The problem is solved! ..and it was my mistake.
Thanks for all the great input from everyone! Filip Cordas pointed me to the problem. The application was terminated before the Foreach loop finished.
Note: The reason for not using FileOptions.DeleteOnClose is that the temp files are created in an extension that I do not have access to and therefore cannot set any file attribute for. I guess you cannot set it for files you didn't create with a file open command, right?
Code:
foreach (string sFile in System.IO.Directory.GetFiles("..\\", "videoBuffer_*.avi"))
{
System.IO.File.Delete(sFile);
}