How to delete folder with all of include(file,folder)?

sarmad

Active member
Joined
Jan 22, 2006
Messages
37
Programming Experience
1-3
hi

i want to delete a folder that have a many file and folders

but if the include file or folder have a attribute (readonly)

i cant delete the folder and the program stop

thanks
 
I would use the IO.Directory.Delete overload (path, recursive=True).
If it throws and IOException you have to recursively reset ReadOnly attributes yourself and call for Delete again.
 
so how i can reset the readonly attribute of all files and folder?

and IO.Directory.Delete overload (path, recursive=True) get syntax error

if you can show the example please

thanks
 
thanks

i sloved it

but for the readonly atrribute ,i cant change it because i dont know how

to change the all of the files and folder attribute

however thanks
 
Set Attributes property of a FileInfo or use the SetAttributes method of File.
 
ok

so
my problem is there
how to apply it to all of the files?

because i dont know how many file will be there
 
That's the beauty of recursion. You create a sub method with path parameter of first (root) folder to take care of your business for all files in that given path. Also, in that method you call the same method using each subdirectory for parameter, each call will of course then automatically call the same method for each subdirectory and so on. By doing one call to this method with root directory for parameter it will by recursion do the same for all directories contained therein.
 
thanks

but i dont know whats your mean


is there a way to set all of the files attribute in the folder?

can you give me the examlpe?

sorry
 
here is some code:
VB.NET:
Sub mytotaldelete()
  Dim mypath As String = "C:\"
  Try
    IO.Directory.Delete(mypath, True)
  Catch ex As IO.IOException
    setattributes(New IO.DirectoryInfo(mypath))
    IO.Directory.Delete(mypath, True)
  End Try
End Sub
 
Sub setattributes(ByVal path As IO.DirectoryInfo)
  For Each d As IO.DirectoryInfo In path.GetDirectories
    setattributes(d)
  Next
  For Each f As IO.FileInfo In path.GetFiles
    f.Attributes = IO.FileAttributes.Archive
  Next
End Sub
 
very very thanks

but also get error
System.UnauthorizedAccessException was unhandled
Message="Access to the path 'New Text Document.txt' is denied."
Source="mscorlib"


and its because of the readonly file 'New Text Document.txt'

so sorry for my problem
 
try changing 'Catch ex As IO.IOException' to 'Catch ex As Exception'
 
Back
Top