The Wayback Machine - https://web.archive.org/web/20240204104729/https://www.geeksforgeeks.org/c-sharp-program-to-check-given-directory-exists-or-not/
Open In App
Related Articles

C# Program to Check Given Directory Exists or not

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 07 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials