tags:

views:

29

answers:

2

Suppose I wish to make a preliminary decision on whether to delete a folder (using a recursive algorithm that will probably manage to delete some files even if the user stupidly tries to delete c:\windows). This is more as a shield for user stupidity rather than some form of automated deletion. I don't care if it disallows deleting stuff that advanced users might want to delete. Some thoughts I've had on detecting that deleting a folder is a bad idea:

  • If folder to be deleted is or contains any folder within Environment.GetFolderPath(Environment.SpecialFolder.*)

  • If folder to be deleted is or is within Environment.GetEnvironmentVariable("systemroot");

  • If DirectoryInfo attributes indicate the directory is a system directory, though I'm not sure that's really used. I do know that on directories, the read-only attribute has been co-opted for other purposes, which is kind of annoying, actually.

Are there any other tests I can do?

A: 

Environment.SpecialFolder.* contains a lot of user-owned data, especially MyDocuments. I'd assume that'd be folder you'd most want to allow deletion within.

Michael Petrotta
Yes, but I don't want to let the user delete MyDocuments itself, or any folder containing MyDocuments.
Brian
@Brian: that sounds good, but contradicts "If folder to be deleted is or contains any folder within Environment.GetFolderPath(Environment.SpecialFolder.*)"
Michael Petrotta
@Michael: No It doesn't. I don't want to delete the My Documents folder. This is the case where folder *is* any folder the collection of special folders. i.e. the My Documents folder itself. I don't want to delete the users directory (e.g. C:\users). This is the case where the folder contains a folder within the collection of special folders (the users directory contains the My Documents folder).
Brian
@Brian: I don't want to get in an argument over this, but consider: `MyDocuments` contains the folder `A`. `A` contains the folder `B`. The user wishes to delete the folder `A`. He is denied, because *"the folder to be deleted (`A`) contains any folder (`B`, in this case) within `Environment.GetFolderPath(Environment.SpecialFolder.*)` (`B` is within MyDocuments)"*. It's possible that we have a different interpretation of that sentence, but I presume you'd want to allow that deletion.
Michael Petrotta
@Michael: Ah, that is one interpretation of my first bullet point, but not actually the interpretation I intended. With the interpretation I intended, `A` does not contain a folder within `Environment.GetFolderPath(Environment.SpecialFolder.*)`. In particular, `B` is not inside `Environment.GetFolderPath(Environment.SpecialFolder.*)`. Since the latter (in my own mental model, not in yours) is a list rather than a variable path, the contains operation is only true for direct matches inside the list.
Brian
A: 

There are other attributes you can check for such as Hidden or ReadOnly.

I also wanted to point out that a lot of the folders in Environment.SpecialFolder point to the folders for the currently logged in user, not necessarily any other users created on the machine.

zdan
Checking for ReadOnly is a bad idea with directories, as it has been co-opted for other purposes.
Brian