开发者

Delete folder with C#

I'm new on C# and I will make a simple too开发者_运维问答l 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?


You can use DirectoryInfo

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("your path");
if (dir.Exists)
     dir.Delete(true);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜