Techumber
Home Blog Work

Basic Folder Operations(Create,Delete,Rename) in C#.NET

Published on October 22, 2012

In BASIC FILE OPERATIONS CREATE,WRITE,READ,APPEND AND DELETE IN C#.NET i have explained how to do basic file operations using C#.NET. Today i am gonna write about Directory Operations using C#.NET. In this post we gonna work on Directory Operations.

  1. Creating Folder
  2. Deleting Folder
  3. Renaming Folder Basic Folder Operations(Create,Delete,Rename) in C#.NET

Creating Folder

using System;
using System.IO;
namespace TU.FolderOpDemo
{
class FolderOp
{
static void Main(string[] args)
{
Console.WriteLine("Please Enter Folder Name:");
string newFolder = Console.ReadLine();
Directory.CreateDirectory(newFolder);
Console.WriteLine("The directory was created!");
Console.ReadKey();
}
}
}

Deleting Folder

using System;
using System.IO;
namespace TU.FolderOpDemo
{
class FolderOp
{
static void Main(string[] args)
{
Console.WriteLine("Please Enter Folder Name You want to delete:");
string Folder = Console.ReadLine();
if(Directory.Exists(Folder))
{
Directory.Delete(Folder);
Console.WriteLine("Folder deleted");
}
Console.ReadKey();
}
}
}

Renaming Folder

using System;
using System.IO;
namespace TU.FolderOpDemo
{
class FolderOp
{
static void Main(string[] args)
{
Console.WriteLine("Please Enter Old Folder Name:");
string oldFolder = Console.ReadLine();

if(Directory.Exists(oldFolder))
{
Console.WriteLine("Please Enter New Folder Name:");
string newFolder = Console.ReadLine();
Directory.Move(oldFolder, newFolder);
if(Directory.Exists(newFolder))
{
Directory.Delete(oldFolder);
Console.WriteLine("The directory is Renamed ");
Console.ReadKey();
}

}
else{
Console.WriteLine("No Such Folder Exits");
}
}
}
}