From b8b89420d8eac840d79a9979cd393d30eeeef0f6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 25 Jun 2021 13:12:08 +0300 Subject: [PATCH] uninstall: Less aggressive directory deletion --- CleanFlashCommon/FileUtil.cs | 41 +++++++++++++++++++++++---------- CleanFlashCommon/Uninstaller.cs | 14 +++++------ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/CleanFlashCommon/FileUtil.cs b/CleanFlashCommon/FileUtil.cs index 9062ec0..38b2a85 100644 --- a/CleanFlashCommon/FileUtil.cs +++ b/CleanFlashCommon/FileUtil.cs @@ -21,12 +21,12 @@ namespace CleanFlashCommon { } public static void RecursiveDelete(DirectoryInfo rootDir, DirectoryInfo baseDir, string filename) { - if (!baseDir.Exists) { + if (!baseDir.FullName.StartsWith(rootDir.FullName)) { + // Sanity check. return; } - if (!baseDir.FullName.StartsWith(rootDir.FullName)) { - // Sanity check. + if (!baseDir.Exists) { return; } @@ -44,15 +44,6 @@ namespace CleanFlashCommon { DeleteFile(file); } } - - if (!Directory.EnumerateFileSystemEntries(baseDir.FullName).Any()) { - try { - baseDir.Delete(); - } catch { - HandleUtil.KillProcessesUsingFile(baseDir.FullName); - baseDir.Delete(); - } - } } public static void DeleteFile(FileInfo file) { @@ -77,6 +68,7 @@ namespace CleanFlashCommon { } HandleUtil.KillProcessesUsingFile(file.FullName); + Thread.Sleep(500); file.Delete(); } } @@ -95,6 +87,31 @@ namespace CleanFlashCommon { RecursiveDelete(dirInfo, dirInfo, null); } + public static void WipeFolder(string baseDir) { + DirectoryInfo dirInfo = new DirectoryInfo(baseDir); + + if (!dirInfo.Exists) { + return; + } + + RecursiveDelete(dirInfo); + + if (!Directory.EnumerateFileSystemEntries(dirInfo.FullName).Any()) { + try { + dirInfo.Delete(); + } catch { + HandleUtil.KillProcessesUsingFile(dirInfo.FullName); + Thread.Sleep(500); + + try { + dirInfo.Delete(); + } catch { + // We've tried for long enough... + } + } + } + } + public static void DeleteFile(string file) { DeleteFile(new FileInfo(file)); } diff --git a/CleanFlashCommon/Uninstaller.cs b/CleanFlashCommon/Uninstaller.cs index 58f98f1..02fa5c3 100644 --- a/CleanFlashCommon/Uninstaller.cs +++ b/CleanFlashCommon/Uninstaller.cs @@ -68,22 +68,22 @@ namespace CleanFlashCommon { public static void DeleteFlashCenter() { // Remove Flash Center from Program Files - FileUtil.RecursiveDelete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "FlashCenter")); + FileUtil.WipeFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "FlashCenter")); if (Environment.Is64BitOperatingSystem) { // Remove Flash Center from Program Files (x86) - FileUtil.RecursiveDelete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "FlashCenter")); + FileUtil.WipeFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "FlashCenter")); } // Remove start menu shortcuts - FileUtil.RecursiveDelete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Microsoft", "Windows", "Start Menu", "Programs", "Flash Center")); + FileUtil.WipeFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Microsoft", "Windows", "Start Menu", "Programs", "Flash Center")); // Remove Flash Center cache and user data - FileUtil.RecursiveDelete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Flash_Center")); + FileUtil.WipeFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Flash_Center")); // Remove shared start menu shortcuts - FileUtil.RecursiveDelete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs", "Flash Center")); - FileUtil.RecursiveDelete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Programs", "Flash Center")); + FileUtil.WipeFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "Programs", "Flash Center")); + FileUtil.WipeFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Programs", "Flash Center")); FileUtil.DeleteFile(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Flash Player.lnk")); // Remove Desktop shortcut @@ -97,7 +97,7 @@ namespace CleanFlashCommon { string parentName = Path.GetFileName(dir); if (parentName.Length == 11 && parentName.EndsWith(".tmp")) { - FileUtil.RecursiveDelete(dir); + FileUtil.WipeFolder(dir); } }