|
|
1.1 ! root 1: using System.IO; ! 2: using System.Linq; ! 3: using System.Security.AccessControl; ! 4: using System.Security.Principal; ! 5: using System.Threading; ! 6: ! 7: namespace CleanFlashCommon { ! 8: public class FileUtil { ! 9: ! 10: public static void TakeOwnership(string filename) { ! 11: FileSecurity security = new FileSecurity(); ! 12: ! 13: SecurityIdentifier sid = WindowsIdentity.GetCurrent().User; ! 14: security.SetOwner(sid); ! 15: security.SetAccessRule(new FileSystemAccessRule(sid, FileSystemRights.FullControl, AccessControlType.Allow)); ! 16: ! 17: File.SetAccessControl(filename, security); ! 18: ! 19: // Remove read-only attribute ! 20: File.SetAttributes(filename, File.GetAttributes(filename) & ~FileAttributes.ReadOnly); ! 21: } ! 22: ! 23: public static void RecursiveDelete(DirectoryInfo rootDir, DirectoryInfo baseDir, string filename) { ! 24: if (!baseDir.FullName.StartsWith(rootDir.FullName)) { ! 25: // Sanity check. ! 26: return; ! 27: } ! 28: ! 29: if (!baseDir.Exists) { ! 30: return; ! 31: } ! 32: ! 33: foreach (DirectoryInfo dir in baseDir.EnumerateDirectories()) { ! 34: RecursiveDelete(rootDir, dir, filename); ! 35: } ! 36: ! 37: foreach (FileInfo file in baseDir.GetFiles()) { ! 38: if (!file.FullName.StartsWith(rootDir.FullName)) { ! 39: // Sanity check. ! 40: continue; ! 41: } ! 42: ! 43: if (filename == null || file.Name.Equals(filename)) { ! 44: DeleteFile(file); ! 45: } ! 46: } ! 47: } ! 48: ! 49: public static void DeleteFile(FileInfo file) { ! 50: if (!file.Exists) { ! 51: return; ! 52: } ! 53: ! 54: try { ! 55: file.IsReadOnly = false; ! 56: file.Delete(); ! 57: } catch { ! 58: for (int i = 0; i < 10; ++i) { ! 59: try { ! 60: TakeOwnership(file.FullName); ! 61: file.IsReadOnly = false; ! 62: file.Delete(); ! 63: return; ! 64: } catch { ! 65: // Try again after sleeping. ! 66: Thread.Sleep(500); ! 67: } ! 68: } ! 69: ! 70: HandleUtil.KillProcessesUsingFile(file.FullName); ! 71: Thread.Sleep(500); ! 72: file.Delete(); ! 73: } ! 74: } ! 75: ! 76: public static void RecursiveDelete(DirectoryInfo baseDir) { ! 77: RecursiveDelete(baseDir, baseDir, null); ! 78: } ! 79: ! 80: public static void RecursiveDelete(string baseDir, string filename) { ! 81: DirectoryInfo dirInfo = new DirectoryInfo(baseDir); ! 82: RecursiveDelete(dirInfo, dirInfo, filename); ! 83: } ! 84: ! 85: public static void RecursiveDelete(string baseDir) { ! 86: DirectoryInfo dirInfo = new DirectoryInfo(baseDir); ! 87: RecursiveDelete(dirInfo, dirInfo, null); ! 88: } ! 89: ! 90: public static void WipeFolder(string baseDir) { ! 91: DirectoryInfo dirInfo = new DirectoryInfo(baseDir); ! 92: ! 93: if (!dirInfo.Exists) { ! 94: return; ! 95: } ! 96: ! 97: RecursiveDelete(dirInfo); ! 98: ! 99: if (!Directory.EnumerateFileSystemEntries(dirInfo.FullName).Any()) { ! 100: try { ! 101: dirInfo.Delete(); ! 102: } catch { ! 103: HandleUtil.KillProcessesUsingFile(dirInfo.FullName); ! 104: Thread.Sleep(500); ! 105: ! 106: try { ! 107: dirInfo.Delete(); ! 108: } catch { ! 109: // We've tried for long enough... ! 110: } ! 111: } ! 112: } ! 113: } ! 114: ! 115: public static void DeleteFile(string file) { ! 116: DeleteFile(new FileInfo(file)); ! 117: } ! 118: } ! 119: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.