Articles → .NET → Exists CreateDirectory and Delete methods of Directory class C#

Exists CreateDirectory and Delete methods

of Directory class C#






Purpose











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



Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250