How to Copy File Using VB.NET

In this article I describe how to copy a file.
  • 5691

Copy a File in VB.NET

The Copy method copies an existing file to a new file on the specified location. The Copy method takes two parameters. First the file name to be copied to with full and the second parameter that is optional that is used to overwrite an existing file. If the second parameter is true, the Copy method will overwrite if file already exists.
The following code snippet copies a file to the destination file.

Imports System.Text

Imports System.IO

Module Module1

    Sub Main()

       Dim fileName As String = "C:\Temp\MaheshTXFITx.txt"

        Dim fi As New IO.FileInfo(fileName)

        Dim destinationFile As String = "C:\Temp\Data\MaheshTXCopied.txt"

        Try

            fi.CopyTo(destinationFile, True)

        Catch iox As IOException

            Console.WriteLine(iox.Message)

        End Try

    End Sub

End Module

 

Above code copies the file from C:\Temp to C:\Temp\Data

 

Output:


CopyFile-in-vb.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.