// [ClassCleanup()] // public static void MyClassCleanup() // { // CleanDirectory(fodderDirectory, null); // } private static void CleanDirectory(string dirToClean, Ionic.CopyData.Transceiver txrx) { if (dirToClean == null) return; if (!Directory.Exists(dirToClean)) return; var dirs = Directory.GetDirectories(dirToClean, "*.*", SearchOption.AllDirectories); if (txrx!=null) txrx.Send("pb 1 max " + dirs.Length.ToString()); foreach (var d in dirs) { CleanDirectory(d, txrx); if (txrx!=null) txrx.Send("pb 1 step"); } // Some of the files are marked as ReadOnly/System, and // before deleting the dir we must strip those attrs. var files = Directory.GetFiles(dirToClean, "*.*", SearchOption.AllDirectories); if (txrx!=null) txrx.Send("pb 1 max " + files.Length.ToString()); foreach (var f in files) { var a = File.GetAttributes(f); // must do ReadOnly bit first - to allow setting other bits. if ((a & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { a &= ~FileAttributes.ReadOnly; File.SetAttributes(f, a); } if (((a & FileAttributes.Hidden) == FileAttributes.Hidden) || ((a & FileAttributes.System) == FileAttributes.System)) { a &= ~FileAttributes.Hidden; a &= ~FileAttributes.System; File.SetAttributes(f, a); } File.Delete(f); if (txrx!=null) txrx.Send("pb 1 step"); } // Delete the directory with delay and retry. // Sometimes I have a console window in the directory // and I want it to not give up so easily. int tries =0; bool success = false; do { try { Directory.Delete(dirToClean, true); success = true; } catch { System.Threading.Thread.Sleep(600); } tries++; } while (tries < 100 && !success); }
private static void DeleteOldFodderDirectories( Ionic.CopyData.Transceiver txrx ) { // Before creating the directory for the current run, Remove old directories. // For some reason the test cleanup code tends to leave these directories?? string tempDir = System.Environment.GetEnvironmentVariable("TEMP"); var oldDirs = Directory.GetDirectories(tempDir, "*.SelectorTests"); if (oldDirs.Length > 0) { if (txrx != null) { txrx.Send("status deleting old directories..."); txrx.Send(String.Format("pb 0 max {0}", oldDirs.Length)); } foreach (var dir in oldDirs) { CleanDirectory(dir, txrx); if (txrx != null) txrx.Send("pb 0 step"); } } }