Arian Azizian Arian Azizian - 1 month ago 23
C# Question

how can delete folders in c sharp

I have some folders and these folders have some text files and i need delete these files but i catch an error with my code!

var dateFolder = Directory.GetDirectories(@"data\stdate").Select(Path.GetDirectoryName).ToArray();
foreach (var dateFile in dateFolder)
{
var stDates =
Directory.GetFiles(@"data\stdate\" + dateFile + "date").Select(Path.GetFileName).ToArray();

foreach (var date in stDates)
{
File.Delete(@"data\stdate\" + dateFile + "date\\" + date);
}
Directory.Delete(@"data\stdate\" + dateFile + "date");
}

SeM SeM
Answer

try this:

Directory.Delete("Path", true); //true: It will delete directory by given path, also folders and files in it.