Articles → CSHARP → Exists Createdirectory And Delete Methods Of Directory Class C#

Exists Createdirectory And Delete Methods Of Directory Class C#






Purpose




  1. Exists – Check if the directory exists or not.
  2. CreateDirectory – Creates the directory on the specified path.
  3. Delete – Deletes the directory at a given path.

Example


using System.IO;
using System;

namespace Sample_Directory {
  class Program {
    static void Main(string[] args) {
      string path = @ "c:\temp\gyansangrah";
      Console.WriteLine("Enter your options here");
      Console.WriteLine("0-to exit");
      Console.WriteLine("1-Create a folder");
      Console.WriteLine("2-Delete a folder");

      int option = Int32.Parse(Console.ReadLine());
      while (option != 0) {
        switch (option) {
        case 1:
          if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
          Console.WriteLine("Directory Created");
          break;
        case 2:
          if (Directory.Exists(path))
            Directory.Delete(path);
          Console.WriteLine("Directory deleted");
          break;
        }
        option = Int32.Parse(Console.ReadLine());
      }
      Console.WriteLine("You have pressed 0. Press any key to close the program");
      Console.ReadLine();
    }
  }
}



Output


Picture showing the output of Exists CreateDirectory and Delete methods of Directory class C#
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, November 27, 2018

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250