1

I'm new on C# and I will make a simple tool that has a button to delete all folders in documents and settings, but not the administrator folders.

Can some one tell me how I can do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sebastian
  • 494
  • 5
  • 18
  • 34
  • Look here: http://bytes.com/topic/c-sharp/answers/245176-how-delete-folder-has-files – OmerGertel Nov 18 '10 at 15:42
  • 3
    How do you tell "administrator folders" apart from normal ones? – Oded Nov 18 '10 at 15:42
  • 7
    I shudder to think what havoc this could raise. Indiscriminately deleting folders in 'Documents and Settings' is a seriously high-risk operation. You're almost certainly going to pull the rug out from under some other application. – Jim Mischel Nov 18 '10 at 15:45
  • @boj - I am local admin on a machine. Guess what - the folder name is **not** "Administrator". – Oded Nov 18 '10 at 15:46
  • @Jim Mischel or out from some other user. – msarchet Nov 18 '10 at 15:53
  • 2
    @matthias: You're eventually going to need [this](http://stackoverflow.com/questions/1124453/how-does-file-recovery-software-work) and [this](http://stackoverflow.com/questions/3813024/deleted-file-recovery-program-using-c-c) and perhaps more. – David Nov 18 '10 at 15:54
  • @ Jim Mischel: is there a better way to delete old profiles? – Sebastian Nov 18 '10 at 15:57
  • @matthias: Manually. Or using any kind of Windows administrative utility. Automated deletes are asking for trouble. How much space do these profiles take up that it's even an issue? How many profiles and machines are we talking? – David Nov 18 '10 at 15:59
  • @ david: mostly the profiles have 1-2gb and i think i will have it on usb to delete it on all pcs – Sebastian Nov 18 '10 at 16:02

3 Answers3

2

You can use DirectoryInfo

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("your path");
if (dir.Exists)
     dir.Delete(true);
bmancini
  • 2,028
  • 15
  • 21
  • 1
    And the requirement to skip "administrator folders"? – Oded Nov 18 '10 at 15:43
  • so under documents and settings i have a lot of profiles...and there are some folders like administrator etc, how can i write it that i do not delete the admin folder...sry for english – Sebastian Nov 18 '10 at 15:46
  • so if(dir.exists && dir.Name != "Administrator"){dir.delete(false)} ?? – Sebastian Nov 18 '10 at 15:51
  • @matthias: if you have only one admin folder (called "Administrator") then yes (with 'true'). but read Jim Mischel's comment thoughtfully. – boj Nov 18 '10 at 15:54
  • @matthias: try existing applications - but in this case this is a superuser.com topic. – boj Nov 18 '10 at 16:48
0

There's a lot of argument going on here, and the answers provided so far will technically work. But let's try a different approach... Why do you want to do this? As you may have surmised by the responses so far, this is probably not a good idea. So maybe with some background on the need being addressed by this software we can perhaps provide more useful answers?

Edit: So you're planning to walk around to each PC with a USB stick and mass-delete? Still doesn't seem like a good approach. Some quick Googling just turned up this, which may work for you. Best part, it works remotely. So that'll remove the "walking around to each PC" part of your task.

David
  • 208,112
  • 36
  • 198
  • 279
  • hello david, the reason is that i will delete all old user profiles with c#...its a lot of work to delete it by hand – Sebastian Nov 18 '10 at 16:00
  • @matthias: I also recommend consulting ServerFault and SuperUser. This sounds like something the Windows network admin should be able to do. (I assume there is one if we're talking about many profiles and many machines, but even if there isn't it still sounds like something for which a tool/process would already exist.) – David Nov 18 '10 at 16:20
-1

You can use System.IO.DirectoryInfo and then call the Delete(true) method to recursively delete all of the Folders and files within your specified folder.

MSDN Directory Info

Now to only delete the non-Administrator folders do you mean the ones owned by administrator or the ones owned by an administrator. Also you won't be able to Delete folders that the current user doesn't own, so you are going to get some exceptions off of this anyways just blindly deleting.

Edit in response to some various comments

You can actually do some iterating over the DirectorySecurity and FileSecurity (I think that's the file one) to figure out the owners group for the Directory or File, and from there determine if it's an admin.

msarchet
  • 15,104
  • 2
  • 43
  • 66