C# Program to check whether a directory exists or not

CsharpProgrammingServer Side Programming

Use the Directory. Exists method to check whether a directory exists or not.

Let’s say you need to check whether the following directory exists or not −

C:\Amit

For that, use the Exists() method −

if (Directory.Exists("C:\Amit")) {
   Console.WriteLine("Directory Amit Exist!");
}

The following is the complete code −

Example

 Live Demo

using System.IO;
using System;
public class Program {
   public static void Main() {
      if (Directory.Exists("C:\Amit")) {
         Console.WriteLine("Directory Amit Exist!");
      } else {
      Console.WriteLine("Directory Amit does not exist!");
      }
   }
}

Output

Directory Amit does not exist!
raja
Updated on 22-Jun-2020 14:35:49

Advertisements