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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • halimunan
    New Member
    • Jun 2007
    • 7

    (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.delet e at the same time.
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by halimunan
    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.delet e at the same time.
    try this...

    [CODE=vb]
    Dim s As String
    For Each s In System.IO.Direc tory.GetFiles(" c:\temp")
    System.IO.File. Delete(s)
    Next s
    [/CODE]

    [CODE=css] string s;
    foreach (s in System.IO.Direc tory.GetFiles(" c:\\temp"))
    {
    System.IO.File. Delete(s);
    }
    [/CODE]

    Comment

    • halimunan
      New Member
      • Jun 2007
      • 7

      #3
      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.SetAc cessControl?

      because everytime i tried to change the status.. it will then become "read only" again...

      Comment

      Working...