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?

9
  • Look here: bytes.com/topic/c-sharp/answers/… Commented Nov 18, 2010 at 15:42
  • 3
    How do you tell "administrator folders" apart from normal ones? Commented Nov 18, 2010 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. Commented Nov 18, 2010 at 15:45
  • @boj - I am local admin on a machine. Guess what - the folder name is not "Administrator". Commented Nov 18, 2010 at 15:46
  • 2
    @matthias: You're eventually going to need this and this and perhaps more. Commented Nov 18, 2010 at 15:54

3 Answers 3

2

You can use DirectoryInfo

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("your path");
if (dir.Exists)
     dir.Delete(true);
Sign up to request clarification or add additional context in comments.

5 Comments

And the requirement to skip "administrator folders"?
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
so if(dir.exists && dir.Name != "Administrator"){dir.delete(false)} ??
@matthias: if you have only one admin folder (called "Administrator") then yes (with 'true'). but read Jim Mischel's comment thoughtfully.
@matthias: try existing applications - but in this case this is a superuser.com topic.
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.

2 Comments

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
@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.)
-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.

1 Comment

And the requirement to skip "administrator folders"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.