private static async Task Clear(this StorageFolder folder, List <FileMetaData> list, ulong maxSize) { if (list == null || !list.Any()) { return; } var total = (ulong)list.Sum(x => (long)x.Size); if (total > maxSize) { try { var file = await folder.GetFileAsync(list.First().Name); await file.DeleteAsync(StorageDeleteOption.PermanentDelete); } catch (FileNotFoundException) { //Already deleted } catch (UnauthorizedAccessException) { //File might be in use. Ignore it and continue with the other files } finally { list.RemoveAt(0); } //Recursive await folder.Clear(list, maxSize).ConfigureAwait(false); } }
public static async Task Clear(this StorageFolder folder, ulong maxSize) { var files = await folder.GetFilesAsync(); var list = new List <FileMetaData>(); foreach (var file in files) { var props = await file.GetBasicPropertiesAsync(); list.Add(new FileMetaData { Name = file.Name, Size = props.Size, Modified = props.DateModified }); } //Order list by Modified date so it deletes old files first list = list.OrderBy(x => x.Modified).ToList(); await folder.Clear(list, maxSize).ConfigureAwait(false); }