C# Path Exists() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Path.Exists() answers one question: is there a file or a folder at this path? It returns true if something is there, and false if not. It was added in .NET 7, so use File.Exists or Directory.Exists in older projects.

Unlike File.Exists (files only) and Directory.Exists (folders only), Path.Exists says true for both. Like them, it never throws: a null, empty, or invalid path simply gives false, and so does a path you are not allowed to see.

This is a static method of System.IO.Path. Call it as Path.Exists(...); there is no new Path(). Unlike most Path methods, Exists does ask the file system. It only reads, so it never creates or changes anything.

Syntax

Pass a path. The result is a bool.

C#
string folder = Path.Combine(Path.GetTempPath(), "codetofun-demo");

bool there = Path.Exists(folder);   // true for a file or a folder

Console.WriteLine(there);
CallWhat it does
Path.Exists(path)true for an existing file or folder, otherwise false.
File.Exists(path)true only for a file.
Directory.Exists(path)true only for a folder.
Path.Exists(null)false. No exception.

The answer is a snapshot. Another program can create or delete the item a moment later, so do not use Exists to guard a file operation. Try the operation and catch the exception instead.

Library declarations
C#
public static bool Exists([NotNullWhen(true)] string? path);

Parameters

ParameterDescription
pathThe path of the file or folder to look for. It may be relative, null, empty, or invalid.

Return Value

Return ValueDescription
booltrue if the path names an existing file or folder. false if it does not, is null or empty, has invalid characters, or cannot be read.

C# Examples

Example 1 — Check a Folder, a File, and a Missing Name

Create a small demo folder with one file, then ask Path.Exists about three paths.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-pathexists-basic");
        if (Directory.Exists(root)) Directory.Delete(root, true);

        Directory.CreateDirectory(root);
        string file = Path.Combine(root, "notes.txt");
        File.WriteAllText(file, "hello");

        Console.WriteLine("Folder: " + Path.Exists(root));
        Console.WriteLine("File: " + Path.Exists(file));
        Console.WriteLine("Missing: " + Path.Exists(Path.Combine(root, "nothing.txt")));

        Directory.Delete(root, true);
        Console.WriteLine("After delete: " + Path.Exists(root));
    }
}

How It Works

  1. A private demo folder. The folder is inside GetTempPath and is removed first if an earlier run left it. Then one file is written into it.
  2. One method, two kinds of thing. Path.Exists is True for the folder and for the file. You do not have to choose between two methods.
  3. A name nobody created. nothing.txt was never written, so the answer is False. The path text was built, but nothing is on the disk.
  4. It follows the disk. After Directory.Delete(root, true), the same check gives False, so the method reads the current state each time you call it.

Example 2 — Path.Exists vs File.Exists vs Directory.Exists

Ask all three methods about a file and about a folder to see where they agree.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-pathexists-compare");
        if (Directory.Exists(root)) Directory.Delete(root, true);

        Directory.CreateDirectory(root);
        string file = Path.Combine(root, "data.txt");
        File.WriteAllText(file, "x");

        Console.WriteLine("file   -> Path: " + Path.Exists(file) + ", File: " + File.Exists(file) + ", Directory: " + Directory.Exists(file));
        Console.WriteLine("folder -> Path: " + Path.Exists(root) + ", File: " + File.Exists(root) + ", Directory: " + Directory.Exists(root));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two real things. The program creates one folder (root) and one file (data.txt) inside it, and asks about both paths.
  2. The file line. File.Exists is True and Directory.Exists is False. Path.Exists is True, because it accepts either kind.
  3. The folder line. The roles are swapped: File.Exists is False and Directory.Exists is True. Path.Exists is still True.
  4. When to use which. Use Path.Exists when either kind is fine. Use the narrow method when a file and a folder with the same name would be a different problem for your code.

Example 3 — Bad Input Gives false, Not an Exception

Pass null, an empty string, a string with only spaces, and a name with an illegal null character.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string missing = null;

        Console.WriteLine("null: " + Path.Exists(missing));
        Console.WriteLine("empty: " + Path.Exists(string.Empty));
        Console.WriteLine("spaces: " + Path.Exists("   "));
        Console.WriteLine("null char: " + Path.Exists("bad\0name"));
        Console.WriteLine("no such folder: " + Path.Exists("no-such-folder-codetofun/file.txt"));
    }
}

How It Works

  1. null and empty. There is nothing to look for, so both give False. A method such as File.ReadAllText would throw for the same input.
  2. Only spaces. It is a legal text on some systems and not on others, but no such item exists, so the answer is False either way.
  3. An illegal character. The null character \0 is not allowed in a path on any system. Exists catches the problem inside and returns False.
  4. A missing parent folder. The last path has a folder that was never created. Exists says False without an exception, so it is safe to call on user input.

Frequently Asked Questions

It returns true when a file or a folder exists at the path, and false otherwise. It never throws, even for null, empty, or invalid paths.
File.Exists is true for files only, Directory.Exists for folders only. Path.Exists is true for either. Pick the narrow one when you need a file or a folder specifically.
It was added in .NET 7. In older versions, such as .NET Framework or .NET 6, call File.Exists or Directory.Exists. If your project targets newer .NET, Path.Exists is safe to use.
No. A null, empty, or invalid path returns false. A path you have no permission to read also gives false, not an exception.
It helps with a clear message, but the file can still disappear before the next line runs. For correct code, call the file method and catch the exceptions it can throw.

Next: GetDirectoryName()

Get the folder part of a path: everything before the last separator.

GetDirectoryName() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful