2

I'm getting an unhandled exception of type System.IO.IOException occurring in mscorlib.dll because the file I'm trying to delete is being used by another process.

I want it to skip the used files.

private void button1_Click(object sender, EventArgs e)
{
    System.IO.DirectoryInfo directory = 
        new System.IO.DirectoryInfo(@"C:\Users\fatih\AppData\Local\Temp");

    foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();
    foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) 
        subDirectory.Delete(true);
}
pillravi
  • 4,035
  • 5
  • 19
  • 33

2 Answers2

7

If it's not important that you delete everything, use:

private void button1_Click(object sender, EventArgs e)
{
    string directory = @"C:\Users\fatih\AppData\Local\Temp";
    deleteDirectory(directory);
}
private void deleteDirectory(string directory){
    foreach (string file in Directory.GetFiles(directory)) 
    {
        try{
             File.Delete(file);
        }
        catch(Exception e0){
        Console.WriteLine(e0.Message+"\n"+e0.Source);//not necessary but nice to learn from
        }
    }
    foreach (string direc in Directory.GetDirectories(directory)) {
          deleteDirectory(direc);
          Directory.Delete(direc,true);
    }
}

This will simply skip over any file or directory that has a problem and delete everything else.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21
-2

Use the static versions of the directory and file methods, you cannot use a foreach on file.delete the way you are doing it because you are trying to do a delete operation on the thing you are looping through.

foreach (var file in Directory.GetFiles(directory))
{
   File.Delete(file);
}

Update: I did not notice the subdirectory delete, that is your issue, if you want to just delete everything including the directory you are on, then you are working too hard.

Directory.Delete(Directory, true); 

Will wipe out the directory and all the subdirectories and files in the directory passed in.

Since you are still having issues, and everyone seems to think I am wrong here, I will give you the code to past into your solution.

private void button1_Click(object sender, EventArgs e)
{
   string directory = @"C:\Users\fatih\AppData\Local\Temp")
   Directory.Delete(directory, true);  
}

IF there is an outside process hanging on to the file this will not work, otherwise it will do what you want it to do

Kelso Sharp
  • 972
  • 8
  • 12
  • If you have an issue with my answer be a man and comment why. Not just mark it down. – Kelso Sharp Sep 08 '16 at 19:38
  • Not you, I know :) – Kelso Sharp Sep 08 '16 at 19:50
  • Did you test my code? I bet you didn't The reason he was getting the error in the first place is because the is trying to delete a file in a foreach loop that is part of the foreach loop, AND that is the same problem with deleting the sub directories, you cannot remove an item from a collection while looping through the same collection, what do you think has a reference to the file??? My completely removes that issue. If you can prove otherwise show me. – Kelso Sharp Sep 08 '16 at 19:57
  • @MichalHainc Also you are wrong, using the Static version of the Directory and File object requires you to pass in the path to the directory and file respectively and therefor have no reference to the directory or file he is trying to delete, so no it will not give him the same error. – Kelso Sharp Sep 08 '16 at 20:05
  • 1
    @Kelso [Directory.GetFiles](https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx) returns a string array, which _is not_ modified real-time as files are deleted, so your argument is invalid – CDspace Sep 08 '16 at 20:15
  • @CDspace yes but notice the capital D on Directory that is the static version of the method, and you are correct, in the asker's question, he is looping through a list of FileInfo object and trying to delete them which you cannot do in a foreach that references the fileinfo collection you are looping on. it throws because you HAVE a reference to the file because its the file you are trying to delete – Kelso Sharp Sep 08 '16 at 20:21
  • 1
    @Kelso and your solution would prevent an `InvalidOperation` which occurs when trying to modify a managed list or the like. Whereas `Directory.GetFiles` still returns an array. I just tried OPs code on a folder without any files opened, and it worked. From the OP _i want it to skip the used files_, which only needs a try-except, like is said in the comments and Shannon's answer. – CDspace Sep 08 '16 at 20:38
  • @CDspace but he doesn't want to skip them, he wants to wipe out everything from that directory and down – Kelso Sharp Sep 08 '16 at 20:48
  • @MichalHainc I never said that it didn't work but its a lot more work than is needed for something simple, did you try my code? He is doing something in 30 lines of code that I was able to do in 2 lines, and his solution at this point leaves temp files still there if there are any references from the foreach loop. – Kelso Sharp Sep 08 '16 at 20:50
  • @Kelso In the post, and a comment, OP explicitly said he want's to skip them, and secondly, if the files are open in another process (the issue at hand, reading the OP again), your method still fails – CDspace Sep 08 '16 at 20:50
  • Hmm... there is an issue with trying to use DirectoryInfo and FileInfo to delete files in a foreach loop, throws a file in use error, even if the file is not in use by another application, in normal circumstances, you should never try and delete files from a directory that other applications use without explicitly identifying the file, and I made that assumption here. Since he is deleting files from a public folder, it very well could be another app holding the reference and I think that over all has been the confusion – Kelso Sharp Sep 08 '16 at 20:59