I'm using the codes below trying to delete files and I got an error that say I'm trying to delete a file that still being used by another process. Please help me how to check which files are being use and skip over that and delete everything else. Here is my code so far, thanks in advance.
foreach (DirectoryInfo Dinfo in dirInfo.GetDirectories("*"))
{
foreach (FileInfo file in Dinfo.GetFiles("*.*"))
{
try
{
file.Delete();
}
catch (FileNotFoundException e)
{
Console.Write(e.FileName.ToString());
}
}
}
Thanks
Jason

Delete Access Denied!!
pamike
mahalax
Yes, I try that and it doesn't delete the files in the "Temporary Internet Files" folder.
crogenicdude
Try:
List<FileInfo> MyFileList = new List<FileInfo>;
foreach (DirectoryInfo Dinfo in dirInfo.GetDirectories("*"))
foreach (FileInfo file in Dinfo.GetFiles("*.*"))
{
try
{
MyFileList.Add(file);
}
catch (FileNotFoundException e)
{
Console.Write(e.FileName.ToString());
}
}
foreach(FileInfo f in MyFileList) f.Delete();
MyFileList.Clear();
Maybe the first bucle is lock its contains files.
Regards.
Madhu Gandhi
Dietz
Polleveys
BillyB
it wouldnt be a fileNotFound exception but rather an UnAuthorizedException in the catch block
so overall the code:
foreach(DirectoryInfo dInfo in dirInfo.GetDirectories("*"))
{
string[] theFiles = System.IO.Directory.GetFiles(dInfo.FullName);
foreach(string currentFile in theFiles)
{
try
{
System.IO.File.Delete(currentFile);
}
catch (UnAuthorizedAccessException ex)
{
//handle if you like
}
catch (IOException ex)
{
//handle if you like
}
}
}
Michael Dyrnaes
Hi,
Thanks for the info......but I have some .wma in the "Temporary Internet Files" folder and it not being use by any process nor it a Window file, it just some file downloaded from the internet. I can right click and delete it, but I want to write a C# code that will delete those files in my "Temporary Internet Files" folder. Can I do that
Thanks in advance!!
Jason
Mart1n0
Hi,
Thanks for the reply, I try the codes and it worked great, thanks again....by the way, why can't I delete files in "Temporary Internet Files" folder but I can in other folders What I have to do to be able to use the above codes to delete files in "Temporary Internet Files" folder
Thanks in advance!!
Jason
Delmonster