System.IO.Directory.Delete Method just sends files to Recycling Bin. Any way to permanently delete?

5.7k Views Asked by At

In .NET, I'm running the code Directory.Delete(tempdir, true); to permanently delete a directory and all contained files. This delete method only sends the files to recycling bin however, instead of permanently deleting them. Is there any way to force Directory.Delete to perma-delete the directory and contained files instead of just moving to recycle bin? I couldn't find any other method overloads to do this.

EDIT: As Neil pointed out, this is not what is actually happening. Directory.Delete is indeed permanently deleting the directory and contained files and not sending them to Recycle Bin. Sorry for any confusion

2

There are 2 best solutions below

3
On BEST ANSWER

Have a look at MSDN:FileSystem.DeleteDirectory.
It has the parameter RecycleOption recycle which uses the enum with the following options:

  • DeletePermanently
  • SendToRecycleBin

So calling the following would be sufficient:

My.Computer.FileSystem.DeleteDirectory(
  DirectoryPath,
  FileIO.UIOption.AllDialogs,
  FileIO.RecycleOption.DeletePermanently); 
2
On

Are you sure this is actually what is happening? There is nothing on the MSDN docs about this behaviour (and it wouldn't be very cross-platform if it did). Using system functionality like recycle bin usually involves some more complicated API functions than a simple delete.

This link implies the exact opposite of your question.