Articles → CSHARP → Exists Createdirectory And Delete Methods Of Directory Class C#
Exists Createdirectory And Delete Methods Of Directory Class C#
Purpose
- Exists – Check if the directory exists or not.
- CreateDirectory – Creates the directory on the specified path.
- 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
Click to Enlarge