private void CleanupFolder(DirectoryInfo folder) { CleanFolderResult result = _ioHelper.CleanFolder(folder, _age); switch (result.Status) { case CleanFolderResultStatus.FailedAsDoesNotExist: _logger.LogDebug("The cleanup folder doesn't exist {Folder}", folder.FullName); break; case CleanFolderResultStatus.FailedWithException: foreach (CleanFolderResult.Error error in result.Errors !) { _logger.LogError(error.Exception, "Could not delete temp file {FileName}", error.ErroringFile.FullName); } break; } folder.Refresh(); // In case it's changed during runtime if (!folder.Exists) { _logger.LogDebug("The cleanup folder doesn't exist {Folder}", folder.FullName); return; } FileInfo[] files = folder.GetFiles("*.*", SearchOption.AllDirectories); foreach (FileInfo file in files) { if (DateTime.UtcNow - file.LastWriteTimeUtc > _age) { try { file.IsReadOnly = false; file.Delete(); } catch (Exception ex) { _logger.LogError(ex, "Could not delete temp file {FileName}", file.FullName); } } } }