Copy a file - Visual Basic .NET

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

To copy a file in vb.net use the following code:


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

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- vb.net

Name the TextBoxes as follow:


SourceText
DestinationText

Name the Buttons as follow:


BrowseSourceButton
BrowseDestinationBtton
CopyButton
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
        fDialog.ShowDialog()
        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
        fDialog.ShowDialog()
        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 visual-basic-tutorials.com 2017 - All Rights Reserved.