VB.net Program to Replace the Content of One File With Another File and Create a Backup File

You are currently viewing VB.net Program to Replace the Content of One File With Another File and Create a Backup File

VB.Net program to replace the content of one file with another file and create a backup file

'VB.Net program to replace the content of one file 
'with another file and create the backup file.

Imports System.IO

Module Module1
    Sub Main()
        Dim str As String = Nothing

        Try
            str = File.ReadAllText("sample.txt")
            Console.WriteLine("Content of sample.txt file: ")
            Console.WriteLine(str)

            str = File.ReadAllText("demo.txt")
            Console.WriteLine("Content of demo.txt file: ")
            Console.WriteLine(str)

            File.Replace("sample.txt", "demo.txt", "backup.txt")

            Console.WriteLine(vbCrLf & vbCrLf & "Content After Replace:" & vbCrLf)
            Console.WriteLine("Content of File(demo.txt):")
            str = File.ReadAllText("demo.txt")
            Console.WriteLine(str)

            Console.WriteLine("Content of File(backup.txt):")
            str = File.ReadAllText("backup.txt")
            Console.WriteLine(str)

        Catch ex As FileNotFoundException
            Console.WriteLine("File does not exist")
        End Try
    End Sub
End Module
Content of sample.txt file:
Hello how are you.
Content of demo.txt file:
it is demo file.


Content After Replace:

Content of File(demo.txt):
Hello how are you.
Content of File(backup.txt):
it is demo file.
Press any key to continue . . .

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply