Introduction
C# DirectoryInfo class provides functionality to work with folders or directories. This article will explain the DirectoryInfo class and its usage with C# code examples. The DirectoryInfo class is declared in the System.IO namespace that must be imported before you can use this class.
C# DirectoryInfo Class
The DirectoryInfo class shares almost all of the same properties as the FileInfo class, except that they operate on directories not files. The DirectoryInfo class does not have static methods and can only be used on instantiated objects. The DirectoryInfo object represents a physical directory on a disk. It also provides instance methods for deleting folders, creating a new directory and sub directories.
When to use C# DirectoryInfo class
If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of the Directory class.
Some of the most useful methods of the DirectoryInfo class are:
| S.No | Methods | Description |
| 1 | Create | This method is used to create the new directory. |
| 2 | CreateSubdirectory | This method is used to create a subdirectory or subdirectories on the specified path. |
| 3 | MoveTo | Moves a DirectoryInfo instance and its contents to a new path. |
| 4 | Delete | Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files. |
| 5 | GetDirectories | This method is used to get the sub directories in the given directory path. |
| 6 | GetFiles | The GetFiles method is used to get the files in the specified folder. |
Figure1 DirectoryInfo Methods
Now Let us see the some of the methods in the DirectoryInfo class.
Create a new directory in C#
The Create method is used to create the new directory.
- static void Main(string[] args)
- {
- String path = @"D:\MyTestFile1.txt";
- DirectoryInfo fl = new DirectoryInfo(path);
- fl.Create();
- {
- Console.WriteLine("Directory has been created");
- }
- }
Create SubDirectory in C#
The CreateSubdirectory method is used to create a subdirectory or subdirectories on the specified path.
- static void Main(string[] args)
- {
- String path = @"D:\MyTestFile1.txt";
- DirectoryInfo fl = new DirectoryInfo(path);
- DirectoryInfo dis = fl.CreateSubdirectory("hellotest");
- {
- Console.WriteLine("Directory has been created");
- }
- }
Move a directory in C#
The MoveTo method of moves a directory to a new location including tts contents.
- static void Main(string[] args)
- {
- String path = @"D:\MyTestFile1.txt";
- string path1 = @"D:\NewFile1.txt";
- DirectoryInfo f1 = new DirectoryInfo(path);
- DirectoryInfo f2 = new DirectoryInfo(path1);
- f1.MoveTo(path1);
- {
- Console.WriteLine("Directory has been Moved");
- }
- }
Delete a directory in C#
The Delete method deletes a directory, specifying whether to delete subdirectories and files.
- static void Main(string[] args) {
- string path = @ "D:\NewFile1.txt";
- DirectoryInfo f1 = new DirectoryInfo(path);
- f1.Delete(); {
- Console.WriteLine("Directory has been deleted");
- }
- }
Get Subdirectories of a directory in C#
GetDirectories method is used to get sub directories in the given directory path, or returns the subdirectories of the current directory.
- static void Main(string[] args) {
- try {
- DirectoryInfo di = new DirectoryInfo(@ "D:\newFile\");
- DirectoryInfo[] dir1 = di.GetDirectories();
- Console.WriteLine("The number of directories containing is {0}.", dir1.Length);
- }
- catch (Exception e) {
- Console.WriteLine("The process failed: {0}", e.ToString());
- }
- }
Get all files in a folder in C#
The GetFiles method is used to get the files in the specified folder. The following code loops through all directories and gets sub directories and loops through them to get all files.
Pankajkumar PatelPosted Sep 3, 2019, 12:09 AM
Nice article