public void CleanUp(int numberOfFilesToTolerate = 0, bool throwAllErrors = true) { if (_isClean) { return; } if (SkipCleaning) { return; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); var di = new DirectoryInfo(_lastDirectory); var fis = di.GetFiles("Log.Unittest.Error.txt", SearchOption.AllDirectories); if (fis.Length > 0) { string s; using (var sr = new StreamReader(fis[0].FullName)) { s = sr.ReadToEnd(); } throw new LPGException(s); } if (Logger.Get().Errors.Count > 0 && throwAllErrors) { Logger.Get().ThrowAllErrors(); } var tryCount = 0; Exception lastException = null; _isClean = true; while (tryCount < 300) { try { if (tryCount > 0) { _filecount = 0; RecursiveDelete(new DirectoryInfo(_lastDirectory)); Logger.ImportantInfo("Remaining file count:" + _filecount); if (_filecount < numberOfFilesToTolerate) { return; } } if (_lastDirectory.Length != 0) { if (Directory.Exists(_lastDirectory)) { Directory.Delete(_lastDirectory, true); Thread.Sleep(1000); } } return; } catch (Exception e) { lastException = e; tryCount++; bool logtoFile = Logger.LogToFile; Logger.LogToFile = false; Logger.Error("File blocked for " + tryCount + "s... waiting 1s and trying again. File" + e.Message); Logger.LogToFile = logtoFile; Thread.Sleep(1000); } } if (lastException != null) { Logger.Exception(lastException); throw lastException; } }