Basics Operations of File and Directory in C#
Last Updated :
13 Sep, 2021
In this article, we are going to cover how to create, delete and rename directory and also how to delete and rename the file.
Creating a Directory
We can create Directory using CreateDirectory() method present in the Directory class.
csharp
using System;
using System.IO;
class Program {
static void Main( string [] args)
{
Console.WriteLine( "Please enter a name for the new directory:" );
string DirName = Console.ReadLine();
if (DirName != String.Empty)
{
Directory.CreateDirectory(DirName);
if (Directory.Exists(DirName))
{
Console.WriteLine( "The directory was created!" );
Console.ReadKey();
}
}
}
}
|
Output:
You will find the Directory with a given name at the specific location.
Renaming a Directory
csharp
using System;
using System.IO;
class GFG {
static void Main( string [] args)
{
Console.WriteLine( "Please enter a name of the directory to renamed:" );
string DirName = Console.ReadLine();
if (Directory.Exists(DirName))
{
Console.WriteLine( "Please enter a new name for this directory:" );
string newDirName = Console.ReadLine();
if (newDirName != String.Empty) {
Directory.Move(DirName, newDirName);
if (Directory.Exists(newDirName))
{
Console.WriteLine( "The directory was renamed to " + newDirName);
Console.ReadKey();
}
}
}
}
}
|
Output:
You will find the updated Directory’s name at the specific location. There is no such method called Rename() so we are using the Move() method to rename a directory. Moving and renaming are the same operations in C#.
Deleting a Directory
csharp
using System;
using System.IO;
class GFG {
static void Main( string [] args)
{
Console.WriteLine( "Enter the directory name you want to delete:" );
string DirName = Console.ReadLine();
if (Directory.Exists(DirName))
{
Directory.Delete(DirName);
if (Directory.Exists(DirName) == false )
Console.WriteLine( "Directory deleted successfully..." );
}
else
Console.WriteLine( "Directory {0} does not exist!" , DirName);
Console.ReadKey();
}
}
|
Output:
You will find the Directory with a given name does not exist anymore at the specified location. If Directory is not empty, then Delete() will throw an exception because it deletes an only empty directory.
Directory.Delete(DirName, true);
If we pass the extra parameters then the Delete() method is recursive. First, all the files and subdirectories of the specified directory are deleted before deleting the directory.
Renaming a File
csharp
using System;
using System.IO;
class GFG {
static void Main( string [] args)
{
Console.WriteLine( "Please enter a name of the file to renamed:" );
string FileName = Console.ReadLine();
if (File.Exists(FileName))
{
Console.WriteLine( "Please enter a new name for this file:" );
string newFilename = Console.ReadLine();
if (newFilename != String.Empty)
{
File.Move(FileName, newFilename);
if (File.Exists(newFilename))
{
Console.WriteLine( "The file was renamed to " + newFilename);
Console.ReadKey();
}
}
}
}
}
|
Output:
You will find the changed file name at the specific location.
Deleting a File
csharp
using System;
using System.IO;
class GFG {
static void Main( string [] args)
{
Console.WriteLine( "Enter the file name you want to delete:" );
string FileName = Console.ReadLine();
if (File.Exists(FileName))
{
File.Delete(FileName);
if (File.Exists(FileName) == false )
Console.WriteLine( "File deleted successfully..." );
}
else
Console.WriteLine( "File {0} does not exist!" , FileName);
Console.ReadKey();
}
}
|
Output:
You will find there is no such file exists with a given name at the specific location.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...