Kamis, 06 Januari 2011

Copy, delete and move files in VB.NET

Public Sub DeleteFilesFromFolders(ByVal sourcePath As String)
        If (Directory.Exists(DirPath)) Then
            For Each fName As String In Directory.GetFiles(DirPath)
                If File.Exists(fName) Then
                    File.Delete(fName)
                End If
            Next
        End If
    End Sub


' This method shows how to move files from one folder to another folder.

    Public Sub MoveFiles(ByVal sourcePath As String, ByVal DestinationPath As String)
        If (Directory.Exists(sourcePath)) Then
            For Each fName As String In Directory.GetFiles(sourcePath)
                If File.Exists(fName) Then
                    Dim dFile As String = String.Empty
                    dFile = Path.GetFileName(fName)
                    Dim dFilePath As String = String.Empty
                    dFilePath = DestinationPath + dFile
                    File.Move(fName, dFilePath)
                End If
            Next
        End If
    End Sub

' This method shows how to copy files from one folder to another folder.

    Public Sub CopyFiles(ByVal sourcePath As String, ByVal DestinationPath As String)
        If (Directory.Exists(sourcePath)) Then
            For Each fName As String In Directory.GetFiles(sourcePath)
                If File.Exists(fName) Then
                    Dim dFile As String = String.Empty
                    dFile = Path.GetFileName(fName)
                    Dim dFilePath As String = String.Empty
                    dFilePath = DestinationPath + dFile
                    File.Copy(fName, dFilePath, True)
                End If
            Next
        End If
    End Sub

Tidak ada komentar:

Posting Komentar