Copy a file - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

To copy a file in use the following code:

My.Computer.FileSystem.CopyFile("C:\yourLocation\", "C:\NewLocation\")

The following project shows you how to make a program that select a file to be copied, then select a destination folder
Create a new form and add 2 TextBoxes, 2 Labels, 3 Buttons:
Copy a file-

Name the TextBoxes as follow:


Name the Buttons as follow:

The code page:
Public Class Form1

    Dim tempFileName As String
    Private Sub BrowseSourceButton_Click(sender As System.Object, e As System.EventArgs) Handles BrowseSourceButton.Click
        Dim fDialog As New OpenFileDialog
        SourceText.Text = fDialog.FileName
        tempFileName = fDialog.SafeFileName
    End Sub
    Private Sub BrowseDestinationBtton_Click(sender As System.Object, e As System.EventArgs) Handles BrowseDestinationBtton.Click
        Dim fDialog As New FolderBrowserDialog
        DestinationText.Text = fDialog.SelectedPath & "\" & tempFileName
    End Sub
    Private Sub CopyButton_Click(sender As System.Object, e As System.EventArgs) Handles CopyButton.Click
        If SourceText.Text.Length > 0 And DestinationText.Text.Length > 0 Then
            My.Computer.FileSystem.CopyFile(SourceText.Text, DestinationText.Text)
            MsgBox("Successfully Copied")
        End If
    End Sub
End Class

Share This

Home | About | Contact | Privacy Policy

Copyright 2017 - All Rights Reserved.