File.Exists() / File.Delete() / File.Copy()
How to use File.Exists() to check if a file exists, File.Delete() to delete a file, File.Copy() to copy a file, and File.Move() to move a file.
Syntax
using System.IO; // Checks whether the file exists (returns true or false). bool exists = File.Exists(string path); // Deletes the file. Does nothing if the file does not exist. File.Delete(string path); // Copies a file. File.Copy(string sourceFileName, string destFileName); // Copies a file (set overwrite to true to allow overwriting). File.Copy(string sourceFileName, string destFileName, bool overwrite); // Moves a file (can also be used to rename a file). File.Move(string sourceFileName, string destFileName); // Moves a file (set overwrite to true to allow overwriting; .NET 5 and later). File.Move(string sourceFileName, string destFileName, bool overwrite);
Method List
| Method | Description |
|---|---|
| File.Exists(path) | Returns true if a file exists at the specified path, or false if it does not. |
| File.Delete(path) | Deletes the specified file. Does not throw an exception if the file does not exist. |
| File.Copy(src, dest) | Copies src to dest. Throws an IOException if dest already exists. |
| File.Copy(src, dest, overwrite) | Set overwrite to true to allow overwriting an existing file. |
| File.Move(src, dest) | Moves a file to a different location. You can also use this to rename a file by changing the file name. |
| File.GetLastWriteTime(path) | Returns the last write time of the file as a DateTime value. |
Sample Code
using System;
using System.IO;
string sourceFile = "original.txt";
string copyDest = "copy.txt";
string moveDest = "moved.txt";
// Create a test file.
File.WriteAllText(sourceFile, "This is a test file.");
// Use File.Exists() to check whether a file exists.
Console.WriteLine(File.Exists(sourceFile)); // True
Console.WriteLine(File.Exists("none.txt")); // False
// Use File.Copy() to copy a file.
File.Copy(sourceFile, copyDest);
Console.WriteLine($"Copy done: {File.Exists(copyDest)}"); // True
// To overwrite an existing file, pass overwrite: true.
File.Copy(sourceFile, copyDest, overwrite: true);
Console.WriteLine("Overwrite copy done");
// Use File.Move() to move (or rename) a file.
File.Move(copyDest, moveDest);
Console.WriteLine($"Copy dest after move: {File.Exists(copyDest)}"); // False
Console.WriteLine($"Move dest after move: {File.Exists(moveDest)}"); // True
// Get the last write time of a file.
DateTime lastWrite = File.GetLastWriteTime(sourceFile);
Console.WriteLine($"Last modified: {lastWrite:yyyy/MM/dd HH:mm:ss}");
// Use File.Delete() to delete a file.
File.Delete(sourceFile);
File.Delete(moveDest);
Console.WriteLine($"After delete: {File.Exists(sourceFile)}"); // False
Notes
File.Exists() is subject to race conditions (TOCTOU). Another process may delete the file between the existence check and the subsequent operation, so it is recommended to combine exception handling with any existence check.
File.Delete() does nothing if the file does not exist, but throws an UnauthorizedAccessException if the file is read-only. For reading and writing an entire file at once, see File.ReadAllText() / WriteAllText(). For line-by-line stream processing, see StreamReader / StreamWriter.
If you find any errors or copyright issues, please contact us.