1

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

Skylar Kennedy
  • 357
  • 1
  • 8
  • 18

2 Answers2

10

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.

Community
  • 1
  • 1
Neil
  • 11,059
  • 3
  • 31
  • 56
  • 1
    Indeed! I just tested it. `Directory.Delete(path, true);` doesn't move the folder to the recycling bin and instead deletes it straight away. – jAC May 22 '17 at 12:11
  • Actually, yes you're right. I also tested it again and it does seem to permanently delete. I see deleted temp directories in my Recycle Bin though so I'm a bit puzzled. I must have manually deleted some files during unit testing. – Skylar Kennedy May 22 '17 at 12:15
0

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); 
jAC
  • 5,195
  • 6
  • 40
  • 55
NP3
  • 1,114
  • 1
  • 8
  • 15
  • I'm curious about a few things concerning this library: 1.) Does `FileSystem.DeleteDirectory` method work in .NET even though it's from the `Microsoft.VisualBasic` package? and 2.) Does this method delete the files contained within the directory as well? I didn't see any parameters that hinted whether it would or not. – Skylar Kennedy May 22 '17 at 12:00
  • 1
    Sure, VisualBasic is .NET compatible. Yes, it does delete the directory and all files within but you can specify that behaviour (`FileIO.DeleteDirectoryOption.ThrowIfDirectoryNonEmpty`, see the doc). – jAC May 22 '17 at 12:03
  • Ah, thank you! I missed that part in the documentation. Sorry for the trivial question. I'm a bit new to C# so I wasn't sure how compatible .NET and VB were. Cheers! – Skylar Kennedy May 22 '17 at 12:06