Use the File.Replace method to backup files in C#

Use the File.Replace method

This example shows how to use the File.Replace method to make a two-level log backup system. The example Make a log file with multiple overflow versions in C# does something similar. That example is a bit more flexible, although it also requires a bit more work.

The File.Replace method takes three parameters: a file to copy (file1), a destination where it should copy the file (file2), and a backup location where the old copy should be moved (file3). File.Replace discards file3, moves file2 to file3 as a backup, and then copies file1 to file2.

You can use this method to make a two-level backup system where file1 is the current file, file2 is a backup, and file3 is an older backup.

The example program displays the current file and two backup files. Enter some text and click Save to bump the backup files down one level and insert the new text into the current file.

The File.Replace method doesn’t work if the files don’t exist, so the program uses the following code when it loads to ensure that the files are present.

private void Form1_Load(object sender, EventArgs e)
    // Make sure all of the files exist.
    if (!File.Exists("MainFile.txt"))
        File.WriteAllText("MainFile.txt", "");
    if (!File.Exists("Backup1.txt"))
        File.WriteAllText("Backup1.txt", "");
    if (!File.Exists("Backup2.txt"))
        File.WriteAllText("Backup2.txt", "");

    // Display the files.

The code simply uses File.Exists to see if the files exist and uses File.WriteAllText to create any files that are missing.

The Load event handler then calls the following ShowFileContents method to display the files’ initial contents.

// Display the files' contents.
private void ShowFileContents()
    txtFile.Text = File.ReadAllText("MainFile.txt");
    txtBackup1.Text = File.ReadAllText("Backup1.txt");
    txtBackup2.Text = File.ReadAllText("Backup2.txt");

This method uses File.ReadAllText to get and display each of the files’ contents.

When you click the Save button, the following code performs the backup.

// Revise the backups and then write the new value into the file.
private void btnSave_Click(object sender, EventArgs e)
    // Move the backup files.
    File.Replace("MainFile.txt", "Backup1.txt", "Backup2.txt");

    // Write into the main file.
    File.WriteAllText("MainFile.txt", txtComment.Text);

    // Display the files' contents.

    // Clear the input TextBox.

The File.Replace method moves Backup1.txt to Backup2.txt and then moves MainFile.txt to Backup1.txt. In other words, MainFile.txt → Backup1.txt → Backup2.txt → garbage.

The code then writes the new text into MainFile.txt and calls the ShowFileContents method to display the files’ current contents. The method finishes by clearing the input TextBox so it’s ready for you to enter the next piece of text.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in files and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *