Arian Azizian Arian Azizian - 1 year ago 126
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");

Answer Source

try this:

Directory.Delete("Path", true); //true: It will delete directory by given path, also folders and files in it.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download