
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Listing out directories and files using C#
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()
- Related Articles
- Listing out directories and files in Python?
- Generate temporary files and directories using Python
- Listing modified, old and newly created files on Linux using C++
- Delete empty files and directories in Linux
- How to list non-hidden files and directories in windows using Python?
- Skip Hidden Files and Directories During Recursive Copy
- How to Protect Files and Directories from Deleting in Linux
- How to remove files and directories in the Linux operating system using the terminal?\n
- 3 Ways to Permanently and Securely Delete Files and Directories in Linux
- Find the Largest Top 10 Files and Directories On a Linux
- How to compare the files available in two directories using diff command in Linux?
- C Program to list all files and sub-directories in a directory
- How to move a file, group of files, and directories in Linux?
- How to Find a Specific String or Word in Files and Directories in Linux
- Exclude directories while using grep command?

Advertisements