Tuesday, November 1, 2011

DirectoryInfo needs an Empty()

I want to have an empty directory to save the results of some re-runnable process (e.g., one that dumps PNGs of all the controls on a form for documentation purposes).

di = New IO.DirectoryInfo("SomeDirectoryToExistAndBeEmpty")
If di.Exists Then
   For Each fi As IO.FileInfo In di.GetFiles
      fi.Delete()
  
Next
   For Each dir As IO.DirectoryInfo In di.GetDirectories
      dir.Delete(
True)
  
Next
Else
   di.Create()
End If

(in this case there are no read-only files or any special attributes since I am generating and regenerating the files myself)

No comments:

Post a Comment