C# - Directory handling
All the classes that are used to read and write files reside at System.IO namespace.
System.IO supports both file as well as directory handling.
Here we will see the working of DirectoryInfo types.
DirectoryInfo class is used to get information on directories. It is used to create
directories, copy directories, move directories, delete directories and many
more.
There is a unique difference between Directory and DirectoryInfo classes.
Directory -> The directory class has only static methods and the instance of this can not
be created. If you want a single operation to be done on a directory, then you can go with directory class and you can accomplish with out
creating any instance.
DirectoryInfo -> If you want to perform a lot of operations on a directory then it’s better to go
with DircetoryInfo. Here you can create an instance and can work with that.
Following are the commonly used methods that we can see under DirectoryInfo type
Create – Creates a directory
Delete – Deletes a directory
GetDirectories - used to get the sub directories in a specified directory.
GetFiles – used to get the files in a specified directory.
MoveTo – used to move directories.
Commonly used properties of DirectoryInfo
CreationTime – returns directory created date and time
CreationimeUtc – returns directory created date and time in UTC format
Exists – used to check the existence of the directory.
FullName – used to get the full path of the directory
LastAccesstime – returns last access time of the directory
LastAccesstime – returns UTC format of LastAccesstime
LastWriteTime – returns last write time of the directory
Parent – used to get the parent directory
Root – used to get the root portion of a path
Let us see all these one by one with examples.
Creating a directory
Directory.CreateDirectory(@“C:\MyDir” );
This will create a directory called MyDir in C drive.
Deleting a directory
Directory.Delete(@"C:\OldDir");
This will delete the specified directory from the drive.
Before deleting the directory please make sure that the directory is empty.
Directory.Delete(@"C:\OldDir", true);
This wil dete the directory recurrsively. If the boolen value is true, it will delete
all the subfolder in the directory and deletes the directory.
Checking for existing directories
if (Directory.Exists(@"C:\MyDir"))
{
// directory exists
}
else
{
// directory does not exists
}
Directory.Exists return true if the specified directory exists otherwise returns
false.
Creation time
Directory.GetCreationTime(@"E:\MyFolder");
This will return the creation time in datetime format
Current directory
Directory.GetCurrentDirectory()
Returns current working folder of the application
Sub directories
string[] dir = Directory.GetDirectories(@"E:\MyFolder");
this will return all sub directories of specified directory
string[] dir = Directory.GetDirectories(@"E:\MyFolder", "*.*");
*.* is a search pattern to match with.
string[] dir = Directory.GetDirectories(@"E:\MyFolder", "*.*", SearchOption.AllDirectories);
SearchOption.AllDirectories -> returns all directories
SearchOption.TopDirectoryOnly -> returns only top directories
Volume information
Directory.GetDirectoryRoot("C:\MyFolder")
Returns volume information of the drive where the specified directory resides.
Get file in a directory
string[] files = Directory.GetFiles(@"E:\MyFolder","*.*");
this will return all files in the specified directory.
"*.*" is for pattern matchimg
Last access time
DateTime time = Directory.GetLastAccessTime(@"E:\Myfoler")
Returns last access time of the directory
Last write time
DateTime time = Directory.GetLastWriteTime(@"E:\Myfoler")
Returns last write time of the directory
Logical drives
string[] files = Directory.GetLogicalDrives();
Returns logical drive name in the system. These have names like
“<drive name>:\” . Example, c:\ , D:\ etc…
Parent directory
DirectoryInfo info = Directory.GetParent(@"E:\MyFolder");
This will return the parent directory of the specified directory.
Moving directories
Directory.Move(@"C:\Comp\games", @"C:\Comp\movies")
This will move the directory “games” to “movies”.
This is all about handling directories programmatically through C#.
Thanks,
SAN