In the example below I need to remove all folders that were written to over an hour ago, it also returns info on the folders processed and whether they were successfully removed:
if (string.IsNullOrEmpty(RootWorkingFolder) || !Directory.Exists(RootWorkingFolder)) return;
Directory.GetDirectories(RootWorkingFolder)
.Where(f => Directory.GetLastWriteTime(f) < DateTime.Now.AddHours(-1))
.Select(delegate(string f)
{
try
{
Directory.Delete(f, true);
return new {Folder = f, Removed = true};
}
catch (IOException)
{
return new {Folder = f, Removed = false};
}
}).ToList();
The ToList method is to ensure that the query is executed immediately.
Pretty useful to know :-)
No comments:
Post a Comment