- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete all files and folders from a path in C#?
For deleting all the folders and its respective directories we can make us System.IO namespace available in C#. The DirectoryInfo() class provides the details of all sub directories and file in a directory.
Example
Let us consider a directory Demo having two sub directories and has some files like below.
using System.IO; namespace DemoApplication { class Program { static void Main(string[] args) { DirectoryInfo di = new DirectoryInfo(@"D:\Demo"); foreach (DirectoryInfo dir in di.GetDirectories()) { foreach (FileInfo file in dir.GetFiles()) { file.Delete(); } dir.Delete(true); } } } }
Output
output of the above code is
We could see that all the folders and its related files are deleted from the demo directory completely. Here GetDirectories() will fetch all the directories of the root directory (Demo) and GetFiles() will fetch all the files (Demo File 1, Demo File 2) present in that directory.
- Related Articles
- How to delete empty files and folders using PowerShell?
- How to delete hidden files and folders using PowerShell?
- Write a C program to print all files and folders.
- How to get list of all files/folders from a folder in Java?
- How to display files/folders including hidden files/folders in PowerShell?
- How to delete all files in a directory with Python?
- How to get only hidden files and folders in PowerShell?
- How to remove hidden files and folders using Python?
- How to get hidden files and folders using PowerShell?
- How to retrieve files and folders attributes using PowerShell?
- How to change files and folders attributes using PowerShell?
- How to delete folder and sub folders using Java?
- How to copy readonly and hidden files/folders in the PowerShell?
- Java program to delete all the files in a directory recursively (only files)
- How to Zip / Unzip files or folders using PowerShell?

Advertisements