Decide if a Folder should not be deleted (e.g. because it is special)
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开发者_Python百科 purposes, which is kind of annoying, actually.
Are there any other tests I can do?
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.
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.
精彩评论