Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hello,

I have write the code for Delete Temp Files but it is not running properly because it is giving an error "This file cannot access because it is being used", how can we solve this problem.
My code is:-

C#
string tempfolder = Path.GetTempPath();
            string[] tempfiles = Directory.GetFiles(tempfolder, "*.*", SearchOption.AllDirectories);

            //MessageBox .Show ("Deleting files from {0}", tempfolder);

            foreach (string tempfile in tempfiles)
            {
                try
                {
                    System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(tempfolder);
                    foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
                   // directory.Delete(true);
                       // File.Delete(tempfile);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("File could not be deleted: {0}", tempfile);
                }
            }


Please help me, for this problem.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted

Your best solution is to put a Try/Ctach block around the File.Delete() call. This will let you catch the error on the one file you're trying to delete and skip it, continuing with deleting the remaining files in the enumeration. The same is true for subdirectories.
foreach (string filePath in Directory.GetFiles(tempPath, "*.*", SearchOption.AllDirectories))
{
    try
    {
        FileInfo currentFile = new FileInfo(filePath);
        currentFile.Delete();
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Error on file: {0}\r\n   {1}", filePath, ex.Message);
    }
}
 
Share this answer
 
Comments
Ravi Vaghela .Net 9-Aug-14 16:19pm    
Solution 6 working .. thanks a lot
this deletes user temp file.. to delete windows temp file just change the tempPath to "C:\\Windows\\Temp" from Path.getTempPath()
Please check the state of the file before delete, also use "Using" keyword so that it every
variable declare will release when it is out of scope.
 
Share this answer
 
You can't, what you can do is put the try..catch around the Delete:
C#
foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) 
try{ 
   subDirectory.Delete(true); 
} catch{}

A better way is to use the command line :
C#
Process.Start("cmd.exe", "/c del /s /q \"foldernamehere\"");
 
Share this answer
 
Hi,
you have to first close all open files from that particular folder and then delete the folder.
 
Share this answer
 
Comments
[no name] 8-Jul-13 2:41am    
I cant't, because i am deleting temp file when we load c#.net windows application.
MuhammadUSman1 8-Jul-13 21:26pm    
Are You using temp file in your application?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900