How To Delete/Copy/Move A File In VB.NET

Deleting A File

Deleting a file is pretty easy , to delete a file you can use the delete method of System.IO

    Dim DeleteFile As String

        DeleteFile = "C:/users/shim/desktop/test.png"

        If System.IO.File.Exists(DeleteFile) = True Then

            System.IO.File.Delete(DeleteFile)
            MsgBox("File Deleted")

        End If

this is the directory of the file C:/users/shim/desktop/test.png

Copying A File

to copy a file you can use copy method of  System.IO

       Dim CopyFile As String
        Dim CopyTo As String

        CopyFile = "C:/users/shim/desktop/test.png"
        CopyTo = "D:/test.png"

        If System.IO.File.Exists(CopyFile) = True Then

            System.IO.File.Copy(CopyFile, CopyTo)
            MsgBox("File Copied")

        End If

destination of your file C:/users/shim/desktop/test.png
destination to copy your file D:/test.png

Moving A File 

to move a file you can use the move method of System.IO

 Dim MoveFile As String
        Dim MoveTo As String

        MoveFile = "C:/users/shim/desktop/test.png"
        MoveTo = "D:/test.png"

        If System.IO.File.Exists(MoveFile) = True Then

            System.IO.File.Move(MoveFile, MoveTo)
            MsgBox("File Moved")

        End If
     
destination of your file C:/users/shim/desktop/test.png
destination to move the file D:/test.png

2 comments

Post a Comment

Note: Only a member of this blog may post a comment.