using System; using System.Collections.Generic; using System.IO; namespace LDUninstaller.Core.Junk { public static class SafetyBlacklist { private static readonly HashSet BlacklistedDirs = new(StringComparer.OrdinalIgnoreCase); private static readonly List ProtectedPrefixes = new(); private static readonly HashSet BlacklistedRegPaths = new(StringComparer.OrdinalIgnoreCase); private static readonly HashSet ProtectedNames = new(StringComparer.OrdinalIgnoreCase) { "windows", "system32", "syswow64", "winsxs", "microsoft", "windows nt", "windows defender", "windows security", "internet explorer", "microsoft.net", "windowspowershell", "dotnet", "common files", "desktop", "downloads", "documents", "pictures", "music", "videos", "appdata", "locallow", "roaming", "local", "temp", "system volume information", "$recycle.bin", "recovery", "boot", "perflogs", "windowsapps", "package cache", "onedrive" }; private static readonly string DynamicUsersRoot; private static readonly string UserProfileDir; static SafetyBlacklist() { // Windows & System directories string winDir = Environment.GetFolderPath(Environment.SpecialFolder.Windows); AddDir(winDir); AddPrefix(winDir); // Any file/folder inside C:\Windows must NEVER be treated as junk! AddDir(Environment.GetFolderPath(Environment.SpecialFolder.System)); AddDir(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86)); string pf = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); AddDir(pf); if (!string.IsNullOrEmpty(pf)) { AddPrefix(Path.Combine(pf, "WindowsApps")); AddDir(Path.Combine(pf, "Common Files")); } string pf86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); if (!string.IsNullOrEmpty(pf86) && !pf86.Equals(pf, StringComparison.OrdinalIgnoreCase)) { AddDir(pf86); AddPrefix(Path.Combine(pf86, "WindowsApps")); AddDir(Path.Combine(pf86, "Common Files")); } UserProfileDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); AddDir(UserProfileDir); // Dynamic Users parent directory (e.g. D:\Users or C:\Users on any non-standard drive) string? usersParent = Path.GetDirectoryName(UserProfileDir); DynamicUsersRoot = !string.IsNullOrWhiteSpace(usersParent) ? usersParent : @"C:\Users"; AddDir(DynamicUsersRoot); // Core user personal folders AddDir(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); AddDir(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); AddDir(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)); AddDir(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); AddDir(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)); AddDir(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)); // Package Cache string commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); if (!string.IsNullOrEmpty(commonAppData)) { AddPrefix(Path.Combine(commonAppData, "Package Cache")); } // OneDrive Roots string[] oneDriveVars = { "OneDrive", "OneDriveConsumer", "OneDriveCommercial" }; foreach (var v in oneDriveVars) { string? envVal = Environment.GetEnvironmentVariable(v); if (!string.IsNullOrWhiteSpace(envVal) && Directory.Exists(envVal)) { AddDir(envVal); AddDir(Path.Combine(envVal, "Documents")); AddDir(Path.Combine(envVal, "Masaüstü")); AddDir(Path.Combine(envVal, "Desktop")); AddDir(Path.Combine(envVal, "Resimler")); AddDir(Path.Combine(envVal, "Pictures")); } } // Drives roots try { foreach (var drive in DriveInfo.GetDrives()) { AddDir(drive.RootDirectory.FullName); } } catch { } // Registry Core Containers & Blacklists BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows NT"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"); BlacklistedRegPaths.Add(@"HKLM\SYSTEM\CurrentControlSet"); BlacklistedRegPaths.Add(@"HKLM\SYSTEM\CurrentControlSet\Services"); BlacklistedRegPaths.Add(@"HKLM\SYSTEM"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Classes"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Policies"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Policies\Microsoft"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Policies\Microsoft\Windows"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft\Windows"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Policies"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\RegisteredApplications"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\RegisteredApplications"); BlacklistedRegPaths.Add(@"HKCR\*\shellex\ContextMenuHandlers"); BlacklistedRegPaths.Add(@"HKCR\Directory\shellex\ContextMenuHandlers"); BlacklistedRegPaths.Add(@"HKCR\CLSID"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\Classes\CLSID"); BlacklistedRegPaths.Add(@"HKCU\SOFTWARE\Classes\CLSID"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\WOW6432Node\Microsoft"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows"); BlacklistedRegPaths.Add(@"HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion"); } private static void AddDir(string? path) { if (!string.IsNullOrWhiteSpace(path)) { try { BlacklistedDirs.Add(Path.GetFullPath(path).TrimEnd('\\')); } catch { } } } private static void AddPrefix(string? path) { if (!string.IsNullOrWhiteSpace(path)) { try { string norm = Path.GetFullPath(path).TrimEnd('\\') + Path.DirectorySeparatorChar; if (!ProtectedPrefixes.Contains(norm)) ProtectedPrefixes.Add(norm); } catch { } } } public static bool IsDangerousDirectory(string path) { if (string.IsNullOrWhiteSpace(path)) return true; try { string norm = Path.GetFullPath(path).TrimEnd('\\'); // 1. Check exact blacklisted system directories if (BlacklistedDirs.Contains(norm)) return true; // 2. Check protected subfolder prefixes (e.g. C:\Windows\...) string normWithSep = norm + Path.DirectorySeparatorChar; foreach (var pfx in ProtectedPrefixes) { if (normWithSep.StartsWith(pfx, StringComparison.OrdinalIgnoreCase)) return true; } // 3. User exclusion list if (ExclusionManager.IsExcluded(norm)) return true; // 4. Protected base folder names string name = Path.GetFileName(norm); if (ProtectedNames.Contains(name)) return true; // 5. Parent checks: cannot delete drive root, user root, or Users parent var di = new DirectoryInfo(norm); if (di.Parent == null) return true; // e.g. C:\ or D:\ if (di.Parent.FullName.Equals(DynamicUsersRoot, StringComparison.OrdinalIgnoreCase)) return true; // e.g. C:\Users\Username directly if (norm.Equals(UserProfileDir, StringComparison.OrdinalIgnoreCase)) return true; return false; } catch { return true; } } public static bool IsDangerousRegistryPath(string regPath) { if (string.IsNullOrWhiteSpace(regPath)) return true; // Normalize HKLM_32 to standard WOW6432Node representation string norm = regPath.Replace("/", "\\").TrimEnd('\\'); if (norm.StartsWith("HKLM_32\\Software", StringComparison.OrdinalIgnoreCase)) { norm = "HKLM\\SOFTWARE\\WOW6432Node" + norm.Substring("HKLM_32\\Software".Length); } else if (norm.StartsWith("HKLM_32\\", StringComparison.OrdinalIgnoreCase)) { norm = "HKLM\\" + norm.Substring("HKLM_32\\".Length); } // User exclusion check if (ExclusionManager.IsExcluded(norm)) return true; foreach (var bl in BlacklistedRegPaths) { if (norm.Equals(bl, StringComparison.OrdinalIgnoreCase)) return true; } // Root hives or 1-level subkeys under hives (e.g. HKLM\SOFTWARE, HKCU\Software, etc.) int slashCount = 0; for (int i = 0; i < norm.Length; i++) { if (norm[i] == '\\') slashCount++; } if (slashCount <= 1) return true; // HKLM, HKLM\SOFTWARE return false; } } }