Open In App

C# Program to Check Given Directory Exists or not

Last Updated : 07 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a directory, now our task is to check given directory exists or not. So to this task, we use the Exists() method of the Directory class. This method will return true if the given directory exists, otherwise false. 

Syntax:

public static bool Exists (string? Mypath);

Where, Mypath is a parameter of Exists() method of string type. It represents the location or path of the specified directory. Now the Exists method will return true if the given path refers to the existing directory otherwise it will return false.

Return Type: The return type of this method is a boolean that is either true or false. This method will return true if the given Mypath refers to the existing directory otherwise it will return false.

Example 1:

C#




// C# program to check whether the given
// directory exists or not
using System;
using System.IO;
  
class GFG{
  
static void Main()
{
      
    // Check whether the directory named
    // vignan exists or not 
    // Using Exists() method
    if (Directory.Exists("D:/vignan"))
        Console.WriteLine("The Specified directory Exists");
    else
        Console.WriteLine("The specified directory does not Exist");
}
}


Output:

The Specified directory Exists

Example 2:

C#




// C# program to check whether the given
// directory exists or not
using System;
using System.IO;
  
class GFG{
  
static void Main()
{
      
    // Check whether the directory named
    // geeks exists or not 
    // Using Exists() method
    if (Directory.Exists("D:/geeks"))
        Console.WriteLine("Exists");
    else
        Console.WriteLine("Not Exist");
}
}


Output:

Not Exist


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads