|
-
May 31st, 2009, 12:42 AM
#1
Thread Starter
Addicted Member
Last edited by kpmsivachand; May 31st, 2009 at 08:59 AM.
Reason: Resolved
-
May 31st, 2009, 12:57 AM
#2
Hyperactive Member
Re: how to delete a folder ?
 Originally Posted by kpmsivachand
Hello to everyone!
i used the following code to delete a folder:
Code:
System.IO.Directory.Delete("c:\test", True)
if the "test" folder contents, contains normal attributes then no problem in deletion.
But if it contains Readonly attributes then it shows access dined error in deletion.
How can i delete a folder forcefully. In vb6 i used fso to delete a folder forcefully, how can i achieve in vb.net???
Thanks in advance! 
Try this:
Code:
FSO.DeleteFolder(C:\test", True)
EDIT: Nevermind, that does nothing. Try google there are tons of results.
Sorry if my posts are misleading or sometimes rude, I'm just trying to get information so try to help me out. 
-
May 31st, 2009, 01:08 AM
#3
Thread Starter
Addicted Member
Re: how to delete a folder ?
 Originally Posted by CoBHC
Try this:
Code:
FSO.DeleteFolder(C:\test", True)
EDIT: Nevermind, that does nothing. Try google there are tons of results.
Thanks for your quick reply...
But we cannt use file system object direcly vb.net know??? if we can use then what steps i have to follow, guide me...
-
May 31st, 2009, 04:07 AM
#4
Fanatic Member
Re: how to delete a folder ?
Try this :
Code:
Dim str As String = "C:\manh"
If System.IO.Directory.Exists(str) = True Then
System.IO.Directory.Delete(str)
End If
-
May 31st, 2009, 07:07 AM
#5
Re: how to delete a folder ?
Loop through the folder (and subfolders) and remove the readonly attribute on all the files then delete:
Code:
Private Sub DeleteDirectory(ByVal Src As String)
Dim Files() As String = Directory.GetFileSystemEntries(Src)
For Each element As String In Files
If Directory.Exists(element) Then
'the current FileSystemEntry is a directory so we need to recursively call ourself
Call DeleteDirectory(element)
Directory.Delete(element) 'Delete the now empty directory
Else
'the current FileSystemEntry is a file so remove readonly and delete
Try
Dim fi As New FileInfo(Src)
With fi
If (.Attributes And IO.FileAttributes.ReadOnly) = IO.FileAttributes.ReadOnly Then .Attributes = .Attributes Or IO.FileAttributes.ReadOnly
If (.Attributes And IO.FileAttributes.Encrypted) = IO.FileAttributes.Encrypted Then .Attributes = .Attributes Or IO.FileAttributes.Encrypted
fi.Delete()
End With
Catch ex As Exception
MessageBox.Show(element & Environment.NewLine & ex.ToString)
End Try
End If
Next element
Directory.Delete(Src)
End Sub
I haven't had time to test this so it may not work quite right yet, but I'm 98% sure the code's fine as is. Also you'll still need to check that the original (base) folder gets deleted too.
 Originally Posted by manhit45
Try this :
Code:
Dim str As String = "C:\manh"
If System.IO.Directory.Exists(str) = True Then
System.IO.Directory.Delete(str)
End If
That's what he's already tried but doesn't work if there's a readonly file in the folder
Last edited by JuggaloBrotha; May 31st, 2009 at 07:17 AM.
-
May 31st, 2009, 08:33 AM
#6
Thread Starter
Addicted Member
-
May 31st, 2009, 08:56 AM
#7
Thread Starter
Addicted Member
Re: how to delete a folder ?
 Originally Posted by JuggaloBrotha
Loop through the folder (and subfolders) and remove the readonly attribute on all the files then delete:
Code:
Private Sub DeleteDirectory(ByVal Src As String)
Dim Files() As String = Directory.GetFileSystemEntries(Src)
For Each element As String In Files
If Directory.Exists(element) Then
'the current FileSystemEntry is a directory so we need to recursively call ourself
Call DeleteDirectory(element)
Directory.Delete(element) 'Delete the now empty directory
Else
'the current FileSystemEntry is a file so remove readonly and delete
Try
Dim fi As New FileInfo(Src)
With fi
If (.Attributes And IO.FileAttributes.ReadOnly) = IO.FileAttributes.ReadOnly Then .Attributes = .Attributes Or IO.FileAttributes.ReadOnly
If (.Attributes And IO.FileAttributes.Encrypted) = IO.FileAttributes.Encrypted Then .Attributes = .Attributes Or IO.FileAttributes.Encrypted
fi.Delete()
End With
Catch ex As Exception
MessageBox.Show(element & Environment.NewLine & ex.ToString)
End Try
End If
Next element
Directory.Delete(Src)
End Sub
I haven't had time to test this so it may not work quite right yet, but I'm 98% sure the code's fine as is. Also you'll still need to check that the original (base) folder gets deleted too.That's what he's already tried but doesn't work if there's a readonly file in the folder
I changed your code as follows:
Code:
Private Sub DeleteDirectory(ByVal Src As String)
Dim Files() As String = Directory.GetFileSystemEntries(Src)
For Each element As String In Files
If Directory.Exists(element) Then
'the current FileSystemEntry is a directory so we need to recursively call ourself
Call DeleteDirectory(element)
Directory.Delete(element) 'Delete the now empty directory
Else
'the current FileSystemEntry is a file so remove readonly and delete
Try
Dim fi As New FileInfo(element)
With fi
'fi.Attributes = FileAttributes.Normal
MsgBox(element)
Shell("cmd.exe /c" & "del " + Chr(34) + element + Chr(34) + " /a /f")
'If (.Attributes And IO.FileAttributes.ReadOnly) = IO.FileAttributes.ReadOnly Then .Attributes = .Attributes Or IO.FileAttributes.ReadOnly
'If (.Attributes And IO.FileAttributes.Encrypted) = IO.FileAttributes.Encrypted Then .Attributes = .Attributes Or IO.FileAttributes.Encrypted
fi.Delete()
MsgBox("Deleted")
End With
Catch ex As Exception
MessageBox.Show(element & Environment.NewLine & ex.ToString)
End Try
End If
Next element
Directory.Delete(Src)
End Sub
I dont know why u didnt use element variable, thats why it didnt work and used run command to delete readonly files.... Thanks for your help...
-
May 31st, 2009, 10:30 AM
#8
Re: how to delete a folder ?
 Originally Posted by kpmsivachand
I changed your code as follows:
Code:
Private Sub DeleteDirectory(ByVal Src As String)
Dim Files() As String = Directory.GetFileSystemEntries(Src)
For Each element As String In Files
If Directory.Exists(element) Then
'the current FileSystemEntry is a directory so we need to recursively call ourself
Call DeleteDirectory(element)
Directory.Delete(element) 'Delete the now empty directory
Else
'the current FileSystemEntry is a file so remove readonly and delete
Try
Dim fi As New FileInfo(element)
With fi
'fi.Attributes = FileAttributes.Normal
MsgBox(element)
Shell("cmd.exe /c" & "del " + Chr(34) + element + Chr(34) + " /a /f")
'If (.Attributes And IO.FileAttributes.ReadOnly) = IO.FileAttributes.ReadOnly Then .Attributes = .Attributes Or IO.FileAttributes.ReadOnly
'If (.Attributes And IO.FileAttributes.Encrypted) = IO.FileAttributes.Encrypted Then .Attributes = .Attributes Or IO.FileAttributes.Encrypted
fi.Delete()
MsgBox("Deleted")
End With
Catch ex As Exception
MessageBox.Show(element & Environment.NewLine & ex.ToString)
End Try
End If
Next element
Directory.Delete(Src)
End Sub
I dont know why u didnt use element variable, thats why it didnt work and used run command to delete readonly files.... Thanks for your help... 
So let me see if I got this right, you comment out the removal of the readonly file attribute, use vb6 code to shell to the command prompt to delete the file, then let the FileInfo object delete the file again?
Why didn't you just use vb6 to call a batch file instead of using .Net in the first place?
-
May 31st, 2009, 08:51 PM
#9
Thread Starter
Addicted Member
Re: how to delete a folder ?
 Originally Posted by JuggaloBrotha
So let me see if I got this right, you comment out the removal of the readonly file attribute, use vb6 code to shell to the command prompt to delete the file, then let the FileInfo object delete the file again?
Why didn't you just use vb6 to call a batch file instead of using .Net in the first place?
Yes, yes you are correct there is no need to delete a file using FileInfo object, and i dont know why your code didnt delete the empty directories
Here the complete code which delete fully:
Code:
DeleteDirectory("c\test") 'This is will delete sub folder files which have readonly attributes and etc.
System.IO.Directory.Delete("c:\test", True) 'This will delete all empty sub directories
Private Sub DeleteDirectory(ByVal Src As String)
Dim Files() As String = Directory.GetFileSystemEntries(Src)
For Each element As String In Files
If Directory.Exists(element) Then
'the current FileSystemEntry is a directory so we need to recursively call ourself
Dim DirInfo As DirectoryInfo
DirInfo = New DirectoryInfo(element)
DirInfo.Attributes = FileAttributes.Normal 'Here settings normal attributes for directory
Call DeleteDirectory(element)
'Directory.Delete(element) 'Delete the now empty directory
Else
Try
Shell("cmd.exe /c" & "del " + Chr(34) + element + Chr(34) + " /a /f")
Catch ex As Exception
MessageBox.Show(element & Environment.NewLine & ex.ToString)
End Try
End If
Next element
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|