C# Program to check whether a directory exists or not

The Directory.Exists method in C# is used to check whether a specified directory exists on the file system. This method returns a boolean value − true if the directory exists, and false if it does not.

This functionality is essential for file system operations where you need to verify directory existence before performing operations like reading files, creating subdirectories, or moving content.

Syntax

Following is the syntax for the Directory.Exists method −

public static bool Exists(string path)

Parameters

  • path − A string representing the path to the directory to check.

Return Value

Returns true if the directory exists; otherwise, false. Also returns false if the path is null or invalid.

Using Directory.Exists with Absolute Path

The most common usage involves checking for a directory using its absolute path −

using System.IO;
using System;

public class Program {
   public static void Main() {
      string directoryPath = "C:\Temp";
      
      if (Directory.Exists(directoryPath)) {
         Console.WriteLine("Directory exists at: " + directoryPath);
      } else {
         Console.WriteLine("Directory does not exist at: " + directoryPath);
      }
   }
}

The output of the above code is −

Directory does not exist at: C:\Temp

Using Directory.Exists with Relative Path

You can also check for directories using relative paths from the current working directory −

using System.IO;
using System;

public class Program {
   public static void Main() {
      string[] pathsToCheck = { 
         "Documents", 
         "..\ParentFolder", 
         "SubFolder\NestedFolder" 
      };
      
      foreach (string path in pathsToCheck) {
         if (Directory.Exists(path)) {
            Console.WriteLine("Found: " + path);
         } else {
            Console.WriteLine("Not found: " + path);
         }
      }
   }
}

The output of the above code is −

Not found: Documents
Not found: ..\ParentFolder
Not found: SubFolder\NestedFolder

Creating Directory If It Doesn't Exist

A common pattern is to check if a directory exists and create it if it doesn't −

using System.IO;
using System;

public class Program {
   public static void Main() {
      string directoryPath = "C:\TestDirectory";
      
      if (!Directory.Exists(directoryPath)) {
         Console.WriteLine("Directory does not exist. Creating...");
         Directory.CreateDirectory(directoryPath);
         Console.WriteLine("Directory created successfully.");
      } else {
         Console.WriteLine("Directory already exists.");
      }
      
      // Verify creation
      Console.WriteLine("Final check: " + Directory.Exists(directoryPath));
   }
}

The output of the above code is −

Directory does not exist. Creating...
Directory created successfully.
Final check: True

Handling Invalid Paths

The Directory.Exists method handles invalid paths gracefully by returning false instead of throwing exceptions −

using System.IO;
using System;

public class Program {
   public static void Main() {
      string[] testPaths = {
         null,
         "",
         "   ",
         "Invalid|Path<>",
         "C:\ValidPath"
      };
      
      foreach (string path in testPaths) {
         bool exists = Directory.Exists(path);
         string pathDisplay = path ?? "null";
         Console.WriteLine($"Path: '{pathDisplay}' - Exists: {exists}");
      }
   }
}

The output of the above code is −

Path: 'null' - Exists: False
Path: '' - Exists: False
Path: '   ' - Exists: False
Path: 'Invalid|Path<>' - Exists: False
Path: 'C:\ValidPath' - Exists: False

Conclusion

The Directory.Exists method provides a simple and reliable way to check directory existence in C#. It returns false for invalid paths without throwing exceptions, making it safe to use in various scenarios. This method is commonly used before performing directory operations to prevent runtime errors.

Updated on: 2026-03-17T07:04:35+05:30

337 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements