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.
- static void Main(string[] args)
- {
- DirectoryInfo di = new DirectoryInfo(@"D:\newfile");
- DirectoryInfo[] dirs = di.GetDirectories();
- foreach (DirectoryInfo diNext in dirs)
- {
- Console.WriteLine("The number of files in {0} is {1}", diNext, diNext.GetFiles().Length);
- }
- }
Here is a complete example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
-
- namespace Createdirectory
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- String path = @"D:\MyTestFile1.txt";
- DirectoryInfo fl = new DirectoryInfo(path);
- fl.Create();
- {
- Console.WriteLine("Directory has been created");
- }
-
-
- String path = @"D:\MyTestFile1.txt";
- DirectoryInfo fl = new DirectoryInfo(path);
- DirectoryInfo dis = fl.CreateSubdirectory("hellotest");
- {
- Console.WriteLine("Directory has been created");
- }
-
- 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");
- }
-
-
- string path = @"D:\NewFile1.txt";
- DirectoryInfo f1 = new DirectoryInfo(path);
- f1.Delete();
- {
- Console.WriteLine("Directory has been deleted");
- }
-
-
- 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());
- }
-
-
- DirectoryInfo di = new DirectoryInfo(@"D:\newfile");
- DirectoryInfo[] dirs = di.GetDirectories();
- foreach (DirectoryInfo diNext in dirs)
- {
- Console.WriteLine("The number of files in {0} is {1}", diNext,
- diNext.GetFiles().Length);
- }
- }
- }
- }
The DirectoryInfo class provides the following properties that enable you to retrieve information about a directory.
S.No |
Property |
Description |
1 |
CreationTime |
This property returns the creation of the directory date and time. |
2 |
Exists |
It returns whether the directory exists or not as Boolean result. |
3 |
FullName |
It returns the full name of the file from the root directory. |
4 |
Name |
It returns the name of the directory |
5 |
LastAccessTime |
It returns the date and time of the last access time |
6 |
LastWriteTime |
It returns the last file saving date and time |
7 |
Root |
Gets the root portion of a path. |
Figure 2 DirectoryInfo Properties
DirectoryInfo Properties example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace directoryinfoproperty {
- class Program {
- static void Main(string[] args) {
- DirectoryInfo fi = new DirectoryInfo(@ "D:\newfile");
- Console.WriteLine("Directory name is {0} ", fi.Name);
- Console.WriteLine("Directory creation time is {0} ", fi.CreationTime.ToLongTimeString());
- Console.WriteLine("Directory Lastaccesstime is {0} ", fi.LastAccessTime.ToLongDateString());
- Console.WriteLine("Directory exist is: ", fi.Exists);
- Console.WriteLine("Directory LastWriteTime is {0} ", fi.LastWriteTime);
- Console.WriteLine("Directory root is {0} ", fi.Root);
- }
- }
- }
Conclusion
C# DirectoryInfo class is very useful for working with directories. In this article and code samples, we learned how to use various properties and methods of DirectoryInfo to work with directories in C#.