How to programmatically change directory attributes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello.

I'm trying to write a simple VS.NET 2003 VB program to delete files and
directories on my hard drive. If the directory is read only, how do i change
it's attribute so that i can delete it ?

Here is some of my code. It does not error out, it just does not delete the
directories.

Any help would be gratefully appreciated even ideas on another way to do this.
I'm new at all this, so any pointers that any body can give would also be
appreciated.

Thanks,
Tony

Try
Dim dirsb() As String = Directory.GetFiles("C:\Documents and
Settings\Administrator\Local Settings\Temp")
For Each dirx In dirsb
Directory.Delete(dirx, True)
Next
Catch ex As Exception
Exit Try
End Try
 
Something like this should do it

Try
Dim dirsb() As String = Directory.GetFiles("C:\Documents and
Settings\Administrator\Local Settings\Temp")
For Each dirx As String In dirsb
Dim info As New DirectoryInfo(dirx)
info.Attributes = Not FileAttributes.ReadOnly And info.Attributes
Directory.Delete(dirx, True)
Next
Catch ex As Exception
End Try

/claes
 
Back
Top