Results 1 to 9 of 9

Thread: [Resolved] how to delete a folder ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Resolved [Resolved] how to delete a folder ?

    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!
    Last edited by kpmsivachand; May 31st, 2009 at 08:59 AM. Reason: Resolved

  2. #2
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Re: how to delete a folder ?

    Quote Originally Posted by kpmsivachand View Post
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Re: how to delete a folder ?

    Quote Originally Posted by CoBHC View Post
    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...

  4. #4
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    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
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Quote Originally Posted by manhit45 View Post
    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Re: how to delete a folder ?

    First of al thanks to all

    Especialy thanks to JuggaloBrotha , he s the one who understand the problem correctly, now i am going to check your code. Once again thank you JuggaloBrotha ....

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Resolved Re: how to delete a folder ?

    Quote Originally Posted by JuggaloBrotha View Post
    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...

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: how to delete a folder ?

    Quote Originally Posted by kpmsivachand View Post
    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?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Resolved Re: how to delete a folder ?

    Quote Originally Posted by JuggaloBrotha View Post
    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
  •  



Click Here to Expand Forum to Full Width