472,096 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

(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 13115
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 leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.