I recently ran into a problem with My.Computer.FileSystem.DeleteDirectory(). It will not delete read-only files.
I found on Google that I can delete read-only files by changing the file attributes to "Normal". Therefore, I wrote a recursive function as shown below.
Private Sub DeleteDir(ByVal dir As DirectoryInfo)
For Each d In dir.GetDirectories
DeleteDir(d)
Next
For Each f In dir.GetFiles
Try
f.Attributes = FileAttributes.Normal
f.Delete()
Catch ex As Exception
Log(ex.Message)
End Try
Next
dir.Delete(True)
End Sub
Everything seems to be fine, but it would be nice if there My.Computer.FileSystem.DeleteDirectory()was another option for deleting read-only files or there was an easier way to do this.
source
share