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
// C# program to create a directory
using System;
using System.IO;
class Program {
// Main Method
static void Main(string[] args)
{
Console.WriteLine("Please enter a name for the new directory:");
string DirName = Console.ReadLine();
// Checking if string is empty or not
if (DirName != String.Empty)
{
// Creating the Directory
Directory.CreateDirectory(DirName);
// Checking Directory is created
// Successfully or not
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 {
// Main Method
static void Main(string[] args)
{
Console.WriteLine("Please enter a name of the directory to renamed:");
string DirName = Console.ReadLine();
// checking directory exist or not
if (Directory.Exists(DirName))
{
Console.WriteLine("Please enter a new name for this directory:");
string newDirName = Console.ReadLine();
if (newDirName != String.Empty) {
// to rename directory
Directory.Move(DirName, newDirName);
// checking directory has
// been renamed or not
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 {
// Main Method
static void Main(string[] args)
{
Console.WriteLine("Enter the directory name you want to delete:");
string DirName = Console.ReadLine();
// Checking if Directory Exist or not
if (Directory.Exists(DirName))
{
// This will delete the
// Directory if it is empty
Directory.Delete(DirName);
// checking if directory if
// deleted successfully or not
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
// C# Program for Renaming a file
using System;
using System.IO;
class GFG {
// Main Method
static void Main(string[] args)
{
Console.WriteLine("Please enter a name of the file to renamed:");
string FileName = Console.ReadLine();
// Checking File exist or not
if (File.Exists(FileName))
{
Console.WriteLine("Please enter a new name for this file:");
string newFilename = Console.ReadLine();
// Checking if string is null or not
if (newFilename != String.Empty)
{
// Renaming the file
File.Move(FileName, newFilename);
// checking if the file has been
// renamed successfully or not
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 {
// Main Method
static void Main(string[] args)
{
Console.WriteLine("Enter the file name you want to delete:");
string FileName = Console.ReadLine();
// Checking file exists or not
if (File.Exists(FileName))
{
// Deleting the file
File.Delete(FileName);
// Checking if the file is deleted
// successfully or not
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.
Similar Reads
C# Program to Search Directories and List Files Given files and directories, now our task is to search these files and directories using C#. So to do this task we use the following methods: 1. SearchOption: This method is used to specify whether to search the current directory or the current directory with all subdirectories. Syntax: public enum
2 min read
C# Program For Listing the Files in a Directory Given files, now our task is to list all these files in the directory using C#. So to do this task we use the following function and class: DirectoryInfo: It is a class that provides different types of methods for moving, creating, and enumerating through directories and their subdirectories. You ca
2 min read
Basics of FileStream in C# The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare. The Syntax to
4 min read
C# - FileInfo Class Methods In this article, we will explain the FileInfo class, its methods, properties, etc. The System.IO namespace is one of the most important namespaces we used while we are working with files in C#. FileInfo Class:It does not have static methods and it can only be used on instantiated objects. The class
3 min read
C# Program to Get the Path of System Directory Using Environment Class In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information,
2 min read
How to Extract filename from a given path in C# While developing an application that can be desktop or web in C#, such kind of requirement to extract the filename from a given path (where the path can be taken while selecting a file using File Open dialog box or any other sources) can arise. A path may contain the drive name, directory name(s) an
2 min read