Delete Directory and Files

   

  Dim dInfo As New DirectoryInfo(MapPath("images/StudentActivity/"))

 

  DeleteDirectory(dInfo)

 

    ''' <summary>

    ''' Delete directory

    ''' </summary>

    ''' <param name="pdInfo"></param>

    ''' <param name="pUserId"></param>

    ''' <remarks></remarks>

    Private Sub DeleteDirectory(ByVal pdInfo As DirectoryInfo)

        Try

            For Each di As DirectoryInfo In pdInfo.GetDirectories()

                    Try

                        di.Delete(True)

                    Catch ex As Exception

                        DeleteFile(di.FullName, True)

                        If Directory.Exists(di.FullName) Then

                            Directory.Delete(di.FullName, True)

                        End If

                    End Try

            Next

        Catch ex As Exception

            'Throw ex

        End Try

    End Sub

 

 

    ''' <summary>

    ''' delete files

    ''' </summary>

    ''' <param name="directoryPath"></param>

    ''' <param name="searchSubdirectories"></param>

    ''' <remarks></remarks>

    Private Sub DeleteFile(ByVal directoryPath As String, ByVal searchSubdirectories As Boolean)

        Try

            Dim strFilePath As String

            Dim strArrDirs() As String

            Dim strDir As String

 

            strArrDirs = Directory.GetFiles(directoryPath)

 

            For Each strDir In strArrDirs

                strFilePath = Path.Combine(directoryPath, strDir)

                If File.Exists(strFilePath) Then

                    File.SetAttributes(strFilePath, FileAttributes.Normal)

                    File.Delete(strFilePath)

                End If

            Next

 

            If searchSubdirectories Then

                Dim dir As New DirectoryInfo(directoryPath)

                For Each subdirectory As DirectoryInfo In dir.GetDirectories()

                    DeleteFile(subdirectory.FullName, searchSubdirectories)

                Next

            End If

        Catch ex As Exception

            'Throw ex

        End Try

    End Sub

Ebook Download
View all
Learn
View all