The Directory class in C# has many methods to perform operations on directories and sub-directories −
| Sr.No | Method & Description |
|---|---|
| 1 | CreateDirectory(String) Creates all directories and subdirectories in the specified path unless they already exist. |
| 2 | CreateDirectoryDirectorySecurity(String) Creates all the directories in the specified path, unless the already exist, applying the specified Windows security. |
| 3 | Delete(String) Deletes an empty directory from a specified path. |
| 4 | DeleteBoolean(String) Deletes the specified directory and, if indicated, any subdirectories and files in the directory. |
| 5 | EnumerateDirectories(String) Returns an enumerable collection of directory names in a specified path. |
| 6 | EnumerateDirectories(String, String) Returns an enumerable collection of directory names that match a search pattern in a specified path. |
To get the directory names, use the EnumerateDirectories method. Our folder is set using DirectoryInfo class −
DirectoryInfo info = new DirectoryInfo(@"D:/new");
Now find the size −
long totalSize = info.EnumerateFiles().Sum(file => file.Length);
For the directories, use −
info.EnumerateDirectories()