Listing out directories and files using C#

CsharpProgrammingServer Side Programming

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

Sr.NoMethod & Description
1CreateDirectory(String)
Creates all directories and subdirectories in the specified path unless they already exist.
2CreateDirectoryDirectorySecurity(String)
Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.
3Delete(String)
Deletes an empty directory from a specified path.
4DeleteBoolean(String)
Deletes the specified directory and, if indicated, any subdirectories and files in the directory.
5EnumerateDirectories(String)
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 −

info.EnumerateDirectories()
raja
Published on 13-Aug-2018 11:33:17
Advertisements