0

Hi I'm running the following code:

void bar()
{
    var dirInfo = new DirectoryInfo("C:\foo\folder");
    dirInfo.Delete();
}

And at one point I got the following exception:

System.IO.IOException: The process cannot access the file 'C:\foo\folder' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
at bar()

Two things bother me here:

I called DirectoryInfo's Delete(). Why did I get Directory's Delete() in the stack trace without DirectoryInfo's one?

How come the file that is being used has the same path as my folder? Is this an error in the message? Or was there a different error?

Mugen
  • 8,301
  • 10
  • 62
  • 140

1 Answers1

1

To answer the first of your questions, DirectoryInfo.Delete calls Directory.Delete. If you're running in release mode, it's possible the compiler has optimised your code and you're just seeing the underlying call.

Edit: I've just done some tests in VS2013. When my project was a release build compiled for Any CPU I could see the call to DirectoryInfo.Delete before the call to Directory.Delete in the stack trace:

StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound)
   at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost)
   at System.IO.DirectoryInfo.Delete()
   at test.Program.Main(String[] args) in c:\Projects\test\Program.cs:line 21

but when I compiled a release build for x64 it showed only the call to Directory.Delete:

StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound)
   at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost)
   at test.Program.Main(String[] args) in c:\Projects\test\Program.cs:line 22

It does look like some optimisation is going on (incidentally, the code was unchanged between tests - I'm guessing the change in line number is also related somehow to compiler optimisation).

Have a look at this question - directoryinfo delete vs directory delete

To answer your other question - did you have a file in the directory open in another app? There are many reasons why another process may be doing something to that folder.

Community
  • 1
  • 1
Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
  • 1
    I don't see how this answers the OP's question as to why his method call isn't being captured in the stacktrace. – Yuval Itzchakov May 19 '15 at 12:54
  • Well, in the question I linked to the decompiled source shows that the DirectoryInfo.Delete call then calls Directory.Delete - afaik the stack trace isn't showing it due to the compiler optimising his code. – Matt Hogan-Jones May 19 '15 at 12:58
  • 1
    So you assume he's running in release mode and the compiler is optimizing. Even if that's true, you should add that to your answer. – Yuval Itzchakov May 19 '15 at 12:59
  • This does make sense. This code was compiled with optimizations applied. – Mugen May 19 '15 at 12:59
  • @MattJones I'm checking explicitly that the folder has no subitems. It could still make sense that the folder is being used by another process, but the error message looks strange. – Mugen May 19 '15 at 13:00
  • 1
    @Mugen - when I get errors like that for deleting files/folders, I turn to Process Explorer or LockHunter to see what's causing the problem. – Matt Hogan-Jones May 19 '15 at 13:24