File system operations, such as creating file backups, moving processed files, or deleting unnecessary logs, are performed using the static methods of the System.IO.File class.
When performing these operations, exceptions (errors) can easily occur if the target file does not exist or if a file with the same name already exists at the destination. Therefore, it is standard practice to check beforehand using File.Exists.
Table of Contents
- Implementation Sample: Backup and Restoration of Daily Report
- Sample Code
- Execution Result (1st Run: File Exists)
- Execution Result (2nd Run: File Missing)
- Explanation and Technical Points
- File.Copy
- File.Delete
- File.Move
Implementation Sample: Backup and Restoration of Daily Report
The following code implements logic that switches behavior based on file existence:
- If the “Daily Report File (DailyReport.csv)” exists, it copies it to a backup folder and deletes the original.
- Conversely, if the original file is missing but a backup exists, it restores (moves) the file from the backup.
Sample Code
using System;
using System.IO;
public class Program
{
public static void Main()
{
// Target file path
string currentDir = Directory.GetCurrentDirectory();
string sourceFile = Path.Combine(currentDir, "DailyReport.csv");
// Backup destination path
string backupDir = Path.Combine(currentDir, "Backup");
string backupFile = Path.Combine(backupDir, "DailyReport_Bak.csv");
// (Preparation) Create backup folder if it doesn't exist
if (!Directory.Exists(backupDir))
{
Directory.CreateDirectory(backupDir);
}
// ---------------------------------------------------------
// Case 1: Source file exists -> Backup and delete
// ---------------------------------------------------------
if (File.Exists(sourceFile))
{
Console.WriteLine("[Process Start] Source file found. Creating backup.");
// 1. Copy file (source, destination, overwrite flag)
// If overwrite: true, it overwrites even if a file with the same name exists
File.Copy(sourceFile, backupFile, overwrite: true);
Console.WriteLine($"Copy Complete: {Path.GetFileName(backupFile)}");
// 2. Delete the original file
File.Delete(sourceFile);
Console.WriteLine("Deleted original file.");
}
// ---------------------------------------------------------
// Case 2: Source file missing, but backup exists -> Restore
// ---------------------------------------------------------
else if (File.Exists(backupFile))
{
Console.WriteLine("[Process Start] Source file missing. Restoring from backup.");
// 3. Move file (also acts as rename)
// Move(source, destination)
File.Move(backupFile, sourceFile);
Console.WriteLine($"Restore Complete: {Path.GetFileName(sourceFile)}");
}
else
{
Console.WriteLine("[Error] Target file not found anywhere.");
// Create a file for testing
File.WriteAllText(sourceFile, "2025-12-25, No Anomalies");
Console.WriteLine("Created test file. Please re-run.");
}
}
}
Execution Result (1st Run: File Exists)
[Process Start] Source file found. Creating backup.
Copy Complete: DailyReport_Bak.csv
Deleted original file.
Execution Result (2nd Run: File Missing)
[Process Start] Source file missing. Restoring from backup.
Restore Complete: DailyReport.csv
Explanation and Technical Points
1. File.Copy
Use the File.Copy(source, dest, overwrite) method. If you set the third argument overwrite to true, it will forcibly overwrite the destination file even if it already exists. If false (the default), an IOException will be thrown if a file with the same name exists.
2. File.Delete
Use the File.Delete(path) method. This permanently deletes the file at the specified path. It does not go to the Recycle Bin, so it cannot be restored. Use this with caution. Note that if you call this method when the file does not exist, no error will occur; it is simply ignored.
3. File.Move
Use the File.Move(source, dest) method. This is used not only for “moving” a file to a different directory but also for “renaming” a file within the same directory. In .NET Core 3.0 and later, you can specify overwrite: true as a third argument to overwrite the file at the destination if it exists.
