How to delete all files and folders in a directory in C#?
To delete all files and folders in a directory in C#, we will need to iterate through folder content andGetFiles()
and delete one by one, and then iterate through folder and GetDirectories()
and delete one by one.Example
using System;
using System.IO;
public class Example
{
public static void Main()
{
System.IO.DirectoryInfo di = new DirectoryInfo(@"C:\Temp_Folder");
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
}
}