Listing out directories and files using C#

The Directory class in C# has many methods to perform operations on directories and sub-directories −

Sr.NoMethod & Description
Creates all directories and subdirectories in the specified path unless they already exist.
Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.
Deletes an empty directory from a specified path.
Deletes the specified directory and, if indicated, any subdirectories and files in the directory.
Returns an enumerable collection of directory names in a specified path.
6EnumerateDirectories(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 −

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know