private static void TryHandlingError(string directory, int i, Exception e) { if (i == retries - 1) // last try also failed { foreach (var file in Directory.GetFiles(directory, "*", SearchOption.AllDirectories)) { var path = Path.GetFullPath(file); try { File.Delete(path); } catch (UnauthorizedAccessException) { var processesUsingFiles = WhoIsLocking.GetProcessesUsingFile(path); var stringBuilder = new StringBuilder(); stringBuilder.Append("The following processing are locking ").Append(path).AppendLine(); foreach (var processesUsingFile in processesUsingFiles) { stringBuilder.Append("\t").Append(processesUsingFile.ProcessName).Append(' ').Append(processesUsingFile.Id). AppendLine(); } throw new IOException(stringBuilder.ToString()); } } throw new IOException("Could not delete " + Path.GetFullPath(directory), e); } GC.Collect(); GC.WaitForPendingFinalizers(); Thread.Sleep(100); }
public static string ThisFile(string path) { var processesUsingFiles = WhoIsLocking.GetProcessesUsingFile(path); var stringBuilder = new StringBuilder(); stringBuilder.Append("The following processing are locking ").Append(path).AppendLine(); foreach (var processesUsingFile in processesUsingFiles) { stringBuilder.Append("\t").Append(processesUsingFile.ProcessName).Append(' ').Append(processesUsingFile.Id). AppendLine(); } return(stringBuilder.ToString()); }