469,359 Members | 1,638 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,359 developers. It's quick & easy.

(c#) delete all files and folders in a directory recursively

hello all,

how do i delete all folders and files inside a directory while at the same time keeping(not deleting the directory) ? e.g : i want to delete all folders and files inside c:\temp

it seems like i need to use file.delete and directory.delete at the same time.
Jun 21 '07 #1
2 12985
dip_developer
648 Expert 512MB
hello all,

how do i delete all folders and files inside a directory while at the same time keeping(not deleting the directory) ? e.g : i want to delete all folders and files inside c:\temp

it seems like i need to use file.delete and directory.delete at the same time.
try this...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim s As String
  3. For Each s In System.IO.Directory.GetFiles("c:\temp")
  4. System.IO.File.Delete(s)
  5. Next s 
  6.  
Expand|Select|Wrap|Line Numbers
  1.  string s;
  2. foreach (s in System.IO.Directory.GetFiles("c:\\temp"))
  3.  {
  4.      System.IO.File.Delete(s);
  5.  }
  6.  
Jun 21 '07 #2
owh.. good.. it works... thanks man!

it deletes all the files but not folders.. and this maybe due to the "read only" on the folders...
how do i change this? directory.SetAccessControl?

because everytime i tried to change the status.. it will then become "read only" again...
Jun 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Matt | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.