Home »
VB.Net »
VB.Net Programs
VB.Net program to copy the content of one file to another file
By Nidhi Last Updated : November 16, 2024
Copying the content of one file to another file in VB.Net
Here, we will copy the content of one file to another file using the Copy() method of File class.
Program/Source Code:
The source code to copy the content of one file to another file is given below. The given program is compiled and executed successfully.
VB.Net code to copy the content of one file to another file
'VB.Net program to copy the content of one file to another file.
Imports System.IO
Module Module1
Sub Main()
Try
Dim data As String
data = File.ReadAllText("xyz.txt")
Console.WriteLine("Content of xyz.txt :" & vbCrLf & data)
File.Copy("xyz.txt", "abc.txt")
data = File.ReadAllText("abc.txt")
Console.WriteLine("Content of abc.txt : " & vbCrLf & data)
Catch ex As FileNotFoundException
Console.WriteLine("File does not exist")
End Try
End Sub
End Module
Output
Content of xyz.txt :
Hello, How are you?
Content of abc.txt :
Hello, How are you?
Press any key to continue . . .
Explanation
In the above program, we created the module Module1 that contains the Main() function. The Main() function is the entry point for the program. Here, we will create a new and copy the content of the existing file into the newly created file using the Copy() function of the File class. Here, we copied the content of the "xyz.txt" file into the "abc.txt" file.
VB.Net File Handling Programs »