How to programmatically change directory attributes

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
 
C

Claes Bergefall

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top