Directory.Delete
I was just reading in the book about the Directory.Delete
method, and the warning it gives about how dangerous it an be to have it delete the folder structure recursively, like this:
Directory.Delete("Settings2", true);
The warning that follows:
This can be extremely dangerous. You can delete entire file systems in an instant with a poorly written
Directory.Delete
. Use it with extreme caution!
It reminded me of a time where I nearly did that.
We had a solution with something like 500 projects. (Huge, and not something I’d recommend, if you can avoid it.)
We had a script that recursively deleted all the bin
and obj
directories in our solution, to do the “cleanest of cleans” for our solution.
Only, the script was set up to do this deletion starting in the current directory, working downward.
If you’re in a terminal window (PowerShell, cmd.exe, etc.), have used cd
to get to where the script lives, and then run the script with something like, ./RemoveObjAndBin.ps1
, it will do what you expect, and remove all bin
and obj
folders in the solution.
But if you’re a fool of a Took like me, and run it from Windows Explorer as an administrator, the working directory is not the solution, but C:/Windows/system32, and the script will happily scour the System32 directory for bin and obj folders.
Ultimately, I don’t think this caused any serious damage.
I don’t know if there even were any bin
or obj
folders in there (and my current computer does not have any).
But it was all done by the time I realized what had happened, and things could have gone far worse, had it been a different working directory or a different search pattern.