Arian Azizian Arian Azizian - 5 months ago 53
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");


try this:

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