In C#, File.Exists() tells you whether a file is on disk at the given path. It answers with a simple true or false, so you can use it straight inside an if.
The method never throws. A null path, an empty path, a path with bad characters, a folder, or a file you have no permission to see all give false.
This is a static method of System.IO.File. Call it as File.Exists(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Pass the path of the file. Build the path with Path.Combine so it works on Windows, Linux, and macOS.
C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-exists.txt");
bool found = File.Exists(path);
Console.WriteLine(found);
Call
What it does
File.Exists(path)
Returns true only when the path names an existing file.
File.Exists(folderPath)
Returns false. A folder is not a file; use Directory.Exists for folders.
File.Exists(null)
Returns false. No exception for null, empty, or invalid paths.
File.Exists asks the file system once. FileInfo.Exists does the same for an object, but it can show an old answer until you call Refresh().
Library declarations
C#
public static bool Exists(string? path);
Parameters
Parameter
Description
path
The file to check. It may be absolute or relative to the current directory. null, empty, or invalid text just gives false.
Return Value
Return Value
Description
true
The path exists and is a file that the caller is allowed to see.
false
The file is missing, the path is a folder, the path is invalid or null, or access is denied.
C# Examples
Example 1 — Check a File Before and After Creating It
Test a path that does not exist, create the file, and test again.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-exists");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "note.txt");
Console.WriteLine(File.Exists(path));
File.WriteAllText(path, "hi");
Console.WriteLine(File.Exists(path));
File.Delete(path);
Console.WriteLine(File.Exists(path));
Directory.Delete(root, true);
}
}
Output
False
True
False
How It Works
Nothing there yet. The demo folder is fresh, so note.txt is not in it and the first check prints False.
Create, then ask again.WriteAllText makes the file, and the same Exists call now prints True. The method reads the disk each time; it does not remember.
Delete and ask a third time. After File.Delete the answer goes back to False.
Example 2 — Folders, Empty Text, and null All Give False
See that Exists never throws, and that a folder is not a file.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-exists-odd");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
Console.WriteLine(File.Exists(root));
Console.WriteLine(Directory.Exists(root));
Console.WriteLine(File.Exists(""));
Console.WriteLine(File.Exists(null));
Directory.Delete(root, true);
}
}
Output
False
True
False
False
How It Works
A folder is not a file.root is a real folder, yet File.Exists(root) prints False. The next line shows that Directory.Exists is the right tool for folders and prints True.
No exception for empty text. An empty string is not a valid path, but Exists quietly returns False instead of throwing ArgumentException.
No exception for null. Most File methods throw ArgumentNullException for null. Exists is the exception to that rule and returns False.
Example 3 — Use Exists to Choose a Message
Wrap the check in a small method that reports on a file instead of crashing when it is missing.
C#
using System;
using System.IO;
class Program {
static string Describe(string path) {
if (!File.Exists(path)) {
return "missing";
}
return "found: " + File.ReadAllText(path).Length + " characters";
}
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-exists-guard");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "notes.txt");
Console.WriteLine(Describe(path));
File.WriteAllText(path, "hello");
Console.WriteLine(Describe(path));
Directory.Delete(root, true);
}
}
Output
missing
found: 5 characters
How It Works
A small guard.Describe returns early with "missing" when Exists is false, so ReadAllText never runs on a file that is not there.
First call. No file yet, so the text missing is printed. No exception is thrown.
Second call. After WriteAllText saves five characters, the guard passes and the method returns found: 5 characters.
A word of care. The file could vanish between the check and the read. In real programs also be ready to catch FileNotFoundException.
Frequently Asked Questions
No. File.Exists returns false for a folder path. Use Directory.Exists to test for a folder, or Path-based checks if you do not care which kind it is.
No. A null path, an empty path, invalid characters, a too-long path, or missing permission all return false instead of throwing.
Usually the path is wrong (a relative path starts at the current directory), the program has no permission to see the file, or the path points at a folder. File.Exists cannot tell you which one.
It is mostly fine for simple programs, but another process can delete the file between the two calls. For important code, call the read method inside try and catch FileNotFoundException.
File.Exists is a static call that asks the disk right now. FileInfo.Exists is a property on an object, and it may keep an old value until you call Refresh().