File and Folder
File & Folder Operation:
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
In this post we gonna work on Directory Operations.
1) Creating Folder
2) Deleting Folder
3) Renaming Folder
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");
}
}
}
}