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

The File.Replace method takes three parameters: a file to copy (file1), a destination to which to copy it (file2), and a backup location where the old copy should be moved (file3). The documentation says 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 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.
    ShowFileContents();
}

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");
}

The ShowFileContents 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.
    ShowFileContents();

    // Clear the input TextBox.
    txtComment.Clear();
}

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.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.