Home All Groups Group Topic Archive Search About

How to programmatically change directory attributes

Author
10 Mar 2006 11:56 PM
Tony Girgenti
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

Author
13 Mar 2006 3:50 PM
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



Show quoteHide quote
"Tony Girgenti" <TonyGirge***@discussions.microsoft.com> wrote in message
news:ABD680C2-F9F5-4320-A190-7AEC98E51C25@microsoft.com...
> 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
>