Search code examples
c#.netsystem.io.directory

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


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


Solution

  • 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);