using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using LDUninstaller.Core.Algorithms; using LDUninstaller.Core.Models; using Microsoft.Win32; namespace LDUninstaller.Core.Junk { public class JunkScanContext { public List<(string Name, string ImagePath, string DisplayName)> Services { get; set; } = new(); public List<(string RelativePath, string TaskFile, string Content)> ScheduledTasks { get; set; } = new(); public List<(string Hive, string KeyPath, string SubKeyName, string DefaultVal)> AppPaths { get; set; } = new(); public List ShortcutFiles { get; set; } = new(); public static JunkScanContext CreateSnapshot() { var ctx = new JunkScanContext(); // 1. Snapshot Services try { using var servicesKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services"); if (servicesKey != null) { foreach (var svcName in servicesKey.GetSubKeyNames()) { try { using var svc = servicesKey.OpenSubKey(svcName); if (svc == null) continue; string? img = svc.GetValue("ImagePath")?.ToString(); string? disp = svc.GetValue("DisplayName")?.ToString(); if (!string.IsNullOrWhiteSpace(img)) { ctx.Services.Add((svcName, img, disp ?? svcName)); } } catch { } } } } catch { } // 2. Snapshot Scheduled Tasks try { string tasksDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "System32", "Tasks"); if (Directory.Exists(tasksDir)) { foreach (var taskFile in Directory.GetFiles(tasksDir, "*", SearchOption.AllDirectories)) { if (taskFile.Contains(@"\Tasks\Microsoft\Windows", StringComparison.OrdinalIgnoreCase)) continue; string relative = taskFile.Substring(tasksDir.Length).Replace('/', '\\'); if (!relative.StartsWith("\\")) relative = "\\" + relative; string content = ""; try { content = File.ReadAllText(taskFile); } catch { } ctx.ScheduledTasks.Add((relative, taskFile, content)); } } } catch { } // 3. Snapshot Shortcuts var shortcutDirs = new[] { Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory), Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), Environment.GetFolderPath(Environment.SpecialFolder.Programs), Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms), Environment.GetFolderPath(Environment.SpecialFolder.Startup), Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Recent") }; foreach (var dir in shortcutDirs) { if (!Directory.Exists(dir)) continue; try { foreach (var file in Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories)) { string ext = Path.GetExtension(file).ToLowerInvariant(); if (ext == ".lnk" || ext == ".url") { ctx.ShortcutFiles.Add(file); } } } catch { } } return ctx; } } public class JunkManager { public static string BackupDirectory => Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "LDUninstaller", "Backups"); // B1: Expanded stop-words including 3-letter & common tech words private static readonly HashSet StopWords = new(StringComparer.OrdinalIgnoreCase) { "the", "and", "for", "with", "app", "application", "software", "corporation", "corp", "inc", "ltd", "gmbh", "edition", "desktop", "client", "runtime", "support", "installer", "setup", "unins", "uninstall", "windows", "microsoft", "x86", "x64", "32bit", "64bit", "package", "driver", "update", "updater", "service", "git", "win", "pro", "media", "player", "game", "games", "bin", "dev", "net", "hub", "sdk", "tool", "tools", "core", "code", "run", "web", "site", "free", "open", "file", "data", "base", "pack", "soft", "system", "systems", "tech", "one", "all", "new", "home", "user", "work", "play", "view", "edit", "help", "info", "test" }; public List ScanJunk(ApplicationUninstallerEntry entry, JunkScanContext? context = null) { var results = new List(); context ??= JunkScanContext.CreateSnapshot(); // P1: Calculate tokens once per entry and pass to all scanners var (cleanName, cleanPub, tokens) = GetMatchTokens(entry); // 1. Install Location leftover scanner ScanInstallLocation(entry, results); // 2. Common drive junk scanner (AppData, LocalAppData, LocalLow, ProgramData, Saved Games) - Documents excluded from auto-clean (B1) ScanCommonDriveJunk(cleanName, cleanPub, tokens, results); // 3. User root dot-folders & config folders (.appname, .config, .cache) ScanUserProfileDotFolders(tokens, results); // 4. Common Files and Temporary setup caches ScanCommonFilesAndTemp(cleanPub, tokens, results); // 5. Multi-drive analysis & Empty parent publisher folder scanner (B22) ScanAllDrivesForAppAndEmptyParent(entry, cleanPub, tokens, results); // 6. Software Registry Keys (HKLM, HKCU & WOW6432Node) (B5, B14) ScanSoftwareRegistryKeys(cleanName, cleanPub, tokens, results); // 7. Windows Services & Kernel/System Drivers (B6: strictly matching InstallLocation) ScanServicesAndDrivers(entry, context, results); // 8. Windows Scheduled Tasks (Görev Zamanlayıcı) ScanScheduledTasks(entry, cleanPub, tokens, context, results); // 9. Application Launch Paths (App Paths) ScanAppPaths(entry, tokens, results); // 10. Startup Registry Keys (Run & RunOnce) ScanStartupRunKeys(entry, tokens, results); // 11. COM Objects, CLSIDs & Context Menu Shell Extensions ScanShellExtensionsAndCom(entry, tokens, results); // 12. Windows Shell MuiCache ScanMuiCache(entry, results); // 13. Application Compatibility & AppCompatFlags ScanAppCompatFlags(entry, results); // 14. Windows Firewall Rules ScanFirewallRules(entry, results); // 15. Windows Prefetch scanner ScanPrefetch(tokens, results); // 16. Windows Error Reporting / CrashDumps ScanWerDumps(tokens, results); // 17. UserAssist ROT13 execution history scanner ScanUserAssist(tokens, results); // 18. Shortcuts & Recent items (Desktop, Start Menu, Startup, Recent) ScanShortcutsAndRecent(tokens, context, results); // 19. Windows Installer (MSI) Orphaned Products ScanMsiOrphanedProducts(entry, cleanName, results); // 20. Registry Uninstaller Entry Check (if uninstall key still exists) (B14) ScanRegistryUninstallKey(entry, results); // Apply Safety Blacklist filter and adjust Confidence foreach (var item in results) { if (item.Type == JunkType.Directory && SafetyBlacklist.IsDangerousDirectory(item.Path)) { if (item is FileSystemJunkResult fs) { fs.Confidence = ConfidenceLevel.Bad; fs.Selected = false; } } else if (item.Type == JunkType.RegistryKey && SafetyBlacklist.IsDangerousRegistryPath(item.Path)) { if (item is RegistryJunkResult rj) { rj.Confidence = ConfidenceLevel.Bad; rj.Selected = false; } } } return results; } public List ScanMultiple(IEnumerable entries) { var appList = entries.ToList(); if (appList.Count == 0) return new List(); // P3: Snapshot system once for all apps var context = JunkScanContext.CreateSnapshot(); var bag = new ConcurrentBag(); Parallel.ForEach(appList, entry => { var list = ScanJunk(entry, context); foreach (var j in list) bag.Add(j); }); // Deduplicate var all = new List(); var seenPaths = new HashSet(StringComparer.OrdinalIgnoreCase); foreach (var j in bag) { string uniqueKey = $"{j.Type}:{j.Path}"; if (j is RegistryJunkResult r && !string.IsNullOrEmpty(r.ValueName)) uniqueKey += $":{r.ValueName}"; if (!seenPaths.Contains(uniqueKey)) { seenPaths.Add(uniqueKey); all.Add(j); } } return all; } public static (string cleanName, string cleanPublisher, List tokens) GetMatchTokens(ApplicationUninstallerEntry entry) { string rawName = entry.DisplayName ?? string.Empty; string stripped = Regex.Replace(rawName, @"(\bv?\d+(\.\d+)+|\(x86\)|\(x64\)|\(amd64\)|sürümü|version)", " ", RegexOptions.IgnoreCase); string cleanName = Sift4.NormalizeName(stripped).ToLowerInvariant().Trim(); string cleanPub = Sift4.NormalizeName(entry.Publisher ?? string.Empty).ToLowerInvariant().Trim(); var tokens = new List(); // Never add token if it's in StopWords or length < 3 if (cleanName.Length >= 3 && !StopWords.Contains(cleanName)) tokens.Add(cleanName); if (cleanPub.Length >= 3 && !StopWords.Contains(cleanPub)) tokens.Add(cleanPub); foreach (var word in cleanName.Split(' ', StringSplitOptions.RemoveEmptyEntries)) { string w = word.Trim().ToLowerInvariant(); if (w.Length >= 3 && !StopWords.Contains(w) && !tokens.Contains(w)) tokens.Add(w); } if (!string.IsNullOrWhiteSpace(entry.UninstallerFullFilename)) { try { string exeName = Path.GetFileNameWithoutExtension(entry.UninstallerFullFilename).ToLowerInvariant(); if (exeName.Length >= 3 && !exeName.Contains("unins") && !exeName.Contains("setup") && !StopWords.Contains(exeName) && !tokens.Contains(exeName)) tokens.Add(exeName); } catch { } } if (!string.IsNullOrWhiteSpace(entry.InstallLocation)) { try { string folderName = Path.GetFileName(entry.InstallLocation.TrimEnd('\\')).ToLowerInvariant(); if (folderName.Length >= 3 && !StopWords.Contains(folderName) && !tokens.Contains(folderName)) tokens.Add(folderName); } catch { } } return (cleanName, cleanPub, tokens); } public static bool IsWordOrExactMatch(string target, string token) { if (string.IsNullOrWhiteSpace(target) || string.IsNullOrWhiteSpace(token)) return false; if (target.Equals(token, StringComparison.OrdinalIgnoreCase)) return true; // Word boundary match: so "git" matches "git" or "git-client" but NEVER "github" return Regex.IsMatch(target, $@"(^|[\s_\-\.\\]){Regex.Escape(token)}($|[\s_\-\.\\])", RegexOptions.IgnoreCase); } private void ScanInstallLocation(ApplicationUninstallerEntry entry, List results) { if (string.IsNullOrWhiteSpace(entry.InstallLocation) || !Directory.Exists(entry.InstallLocation)) return; if (SafetyBlacklist.IsDangerousDirectory(entry.InstallLocation)) return; results.Add(new FileSystemJunkResult { EntryName = Path.GetFileName(entry.InstallLocation.TrimEnd('\\')), Path = entry.InstallLocation, IsDirectory = true, Confidence = ConfidenceLevel.VeryGood, Reason = "Kaldırma sonrası diskte kalan ana kurulum klasörü", Selected = true }); } private void ScanCommonDriveJunk(string cleanName, string cleanPub, List tokens, List results) { if (tokens.Count == 0) return; // B1: Documents is removed from auto-selected paths to protect user projects / code repositories var targetRoots = new List { Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "AppData", "LocalLow"), Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Saved Games") }; foreach (var root in targetRoots) { if (!Directory.Exists(root)) continue; try { foreach (var dir in Directory.GetDirectories(root)) { string dirName = Path.GetFileName(dir).ToLowerInvariant(); if (SafetyBlacklist.IsDangerousDirectory(dir)) continue; bool matched = false; ConfidenceLevel conf = ConfidenceLevel.Good; if (dirName.Equals(cleanName, StringComparison.OrdinalIgnoreCase)) { matched = true; conf = ConfidenceLevel.VeryGood; } else if (!string.IsNullOrEmpty(cleanPub) && dirName.Equals(cleanPub, StringComparison.OrdinalIgnoreCase)) { // Publisher folder: search subdirectories with strict match! try { foreach (var sub in Directory.GetDirectories(dir)) { string subName = Path.GetFileName(sub).ToLowerInvariant(); if (tokens.Any(t => IsWordOrExactMatch(subName, t) || Sift4.GetSimilarityNormalized(subName, t) >= 0.82)) { results.Add(new FileSystemJunkResult { EntryName = $"{dirName}\\{subName}", Path = sub, IsDirectory = true, Confidence = ConfidenceLevel.VeryGood, Reason = "Yayıncı klasörü altındaki uygulama veri kalıntısı", Selected = true }); } } } catch { } continue; } else if (tokens.Any(t => IsWordOrExactMatch(dirName, t) || Sift4.GetSimilarityNormalized(dirName, t) >= 0.85)) { matched = true; conf = ConfidenceLevel.Good; } if (matched) { results.Add(new FileSystemJunkResult { EntryName = Path.GetFileName(dir), Path = dir, IsDirectory = true, Confidence = conf, Reason = "Kullanıcı verisi ve yapılandırma klasörü kalıntısı", Selected = true }); } } } catch { } } } private void ScanUserProfileDotFolders(List tokens, List results) { if (tokens.Count == 0) return; string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); if (!Directory.Exists(userProfile)) return; try { // %USERPROFILE%\.{token} foreach (var dir in Directory.GetDirectories(userProfile, ".*")) { string dirName = Path.GetFileName(dir); if (SafetyBlacklist.IsDangerousDirectory(dir)) continue; string stripped = dirName.TrimStart('.').ToLowerInvariant(); // B1: Strict word or exact match, never loose substring if (tokens.Any(t => IsWordOrExactMatch(stripped, t))) { results.Add(new FileSystemJunkResult { EntryName = dirName, Path = dir, IsDirectory = true, Confidence = ConfidenceLevel.VeryGood, Reason = "Kullanıcı profil dizinindeki gizli yapılandırma klasörü (Dot-folder)", Selected = true }); } } // %USERPROFILE%\.config and %USERPROFILE%\.cache string[] modernConfigs = { Path.Combine(userProfile, ".config"), Path.Combine(userProfile, ".cache"), Path.Combine(userProfile, ".local", "share") }; foreach (var parent in modernConfigs) { if (!Directory.Exists(parent)) continue; foreach (var dir in Directory.GetDirectories(parent)) { string dirName = Path.GetFileName(dir).ToLowerInvariant(); if (SafetyBlacklist.IsDangerousDirectory(dir)) continue; if (tokens.Any(t => IsWordOrExactMatch(dirName, t) || Sift4.GetSimilarityNormalized(dirName, t) >= 0.85)) { results.Add(new FileSystemJunkResult { EntryName = Path.GetFileName(dir), Path = dir, IsDirectory = true, Confidence = ConfidenceLevel.VeryGood, Reason = "Kullanıcı modern yapılandırma/önbellek klasörü (.config/.cache)", Selected = true }); } } } } catch { } } private void ScanCommonFilesAndTemp(string cleanPub, List tokens, List results) { if (tokens.Count == 0) return; // Common Files var commonDirs = new[] { Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Common Files"), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Common Files") }; foreach (var common in commonDirs) { if (!Directory.Exists(common)) continue; try { foreach (var dir in Directory.GetDirectories(common)) { string dirName = Path.GetFileName(dir).ToLowerInvariant(); if (SafetyBlacklist.IsDangerousDirectory(dir)) continue; if (!string.IsNullOrEmpty(cleanPub) && dirName.Equals(cleanPub, StringComparison.OrdinalIgnoreCase)) { foreach (var sub in Directory.GetDirectories(dir)) { string subName = Path.GetFileName(sub).ToLowerInvariant(); if (tokens.Any(t => IsWordOrExactMatch(subName, t) || Sift4.GetSimilarityNormalized(subName, t) >= 0.80)) { results.Add(new FileSystemJunkResult { EntryName = $"Common Files\\{dirName}\\{subName}", Path = sub, IsDirectory = true, Confidence = ConfidenceLevel.VeryGood, Reason = "Ortak program bileşenleri ve kütüphane kalıntısı (Common Files)", Selected = true }); } } } else if (tokens.Any(t => IsWordOrExactMatch(dirName, t) || Sift4.GetSimilarityNormalized(dirName, t) >= 0.85)) { results.Add(new FileSystemJunkResult { EntryName = $"Common Files\\{dirName}", Path = dir, IsDirectory = true, Confidence = ConfidenceLevel.Good, Reason = "Ortak program bileşenleri klasörü (Common Files)", Selected = true }); } } } catch { } } // Temp Setup / Uninstaller Caches try { string tempDir = Path.GetTempPath(); if (Directory.Exists(tempDir)) { foreach (var dir in Directory.GetDirectories(tempDir)) { string dirName = Path.GetFileName(dir).ToLowerInvariant(); if (SafetyBlacklist.IsDangerousDirectory(dir)) continue; if (tokens.Any(t => IsWordOrExactMatch(dirName, t))) { results.Add(new FileSystemJunkResult { EntryName = dirName, Path = dir, IsDirectory = true, Confidence = ConfidenceLevel.VeryGood, Reason = "Geçici kurulum veya uninstaller önbellek klasörü (Temp)", Selected = true }); } } } } catch { } } private void ScanAllDrivesForAppAndEmptyParent(ApplicationUninstallerEntry entry, string cleanPub, List tokens, List results) { // B22: Empty publisher folder cleanup with strict safety if (!string.IsNullOrWhiteSpace(entry.InstallLocation)) { try { var parent = Directory.GetParent(entry.InstallLocation.TrimEnd('\\')); if (parent != null && parent.Exists && !SafetyBlacklist.IsDangerousDirectory(parent.FullName)) { string parentName = parent.Name.ToLowerInvariant(); string[] forbiddenGenericRoots = { "games", "oyunlar", "program files", "programs", "apps", "steamlibrary", "common" }; // Only consider publisher folder if parent folder name actually matches publisher! bool isPublisherFolder = !string.IsNullOrWhiteSpace(cleanPub) && (parentName.Equals(cleanPub, StringComparison.OrdinalIgnoreCase) || Sift4.GetSimilarityNormalized(parentName, cleanPub) >= 0.85); if (isPublisherFolder && !forbiddenGenericRoots.Contains(parentName)) { var entries = parent.GetFileSystemInfos(); bool isEmptyOrOnlyTarget = entries.Length == 0 || (entries.Length == 1 && entries[0].FullName.Equals(Path.GetFullPath(entry.InstallLocation).TrimEnd('\\'), StringComparison.OrdinalIgnoreCase)); if (isEmptyOrOnlyTarget) { results.Add(new FileSystemJunkResult { EntryName = parent.Name, Path = parent.FullName, IsDirectory = true, Confidence = ConfidenceLevel.VeryGood, Reason = "Uygulama kaldırıldıktan sonra boş kalan ana yayıncı klasörü", Selected = true }); } } } } catch { } } // Secondary Ready Drives (e.g. D:\, E:\) if (tokens.Count > 0) { try { string systemRoot = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.Windows)) ?? "C:\\"; foreach (var drive in DriveInfo.GetDrives()) { if (!drive.IsReady || drive.DriveType != DriveType.Fixed) continue; if (drive.RootDirectory.FullName.Equals(systemRoot, StringComparison.OrdinalIgnoreCase)) continue; string root = drive.RootDirectory.FullName; string[] candidateFolders = { "Games", "Oyunlar", "Program Files", "Programs", "SteamLibrary\\steamapps\\common" }; foreach (var cand in candidateFolders) { string target = Path.Combine(root, cand); if (!Directory.Exists(target)) continue; foreach (var sub in Directory.GetDirectories(target)) { string subName = Path.GetFileName(sub).ToLowerInvariant(); if (SafetyBlacklist.IsDangerousDirectory(sub)) continue; if (tokens.Any(t => IsWordOrExactMatch(subName, t) || Sift4.GetSimilarityNormalized(subName, t) >= 0.85)) { results.Add(new FileSystemJunkResult { EntryName = $"{cand}\\{Path.GetFileName(sub)}", Path = sub, IsDirectory = true, Confidence = ConfidenceLevel.VeryGood, Reason = $"İkincil disk ({drive.Name.TrimEnd('\\')}) üzerindeki oyun veya uygulama kalıntısı", Selected = true }); } } } } } catch { } } } private void ScanSoftwareRegistryKeys(string cleanName, string cleanPub, List tokens, List results) { if (tokens.Count == 0) return; var hives = new[] { (Prefix: "HKCU\\Software", Key: Registry.CurrentUser.OpenSubKey("Software")), (Prefix: "HKLM\\SOFTWARE", Key: Registry.LocalMachine.OpenSubKey("SOFTWARE")), (Prefix: "HKLM_32\\Software", Key: RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("Software")) }; foreach (var (prefix, rootKey) in hives) { if (rootKey == null) continue; using (rootKey) { foreach (var subName in rootKey.GetSubKeyNames()) { string fullPath = $@"{prefix}\{subName}"; if (SafetyBlacklist.IsDangerousRegistryPath(fullPath)) continue; string subLower = subName.ToLowerInvariant(); if (subLower.Equals(cleanName, StringComparison.OrdinalIgnoreCase) || tokens.Any(t => IsWordOrExactMatch(subLower, t))) { results.Add(new RegistryJunkResult { EntryName = subName, Path = fullPath, Confidence = ConfidenceLevel.VeryGood, Reason = "Kayıt defteri yazılım anahtarı kalıntısı", Selected = true }); } else if (!string.IsNullOrEmpty(cleanPub) && (subLower.Equals(cleanPub, StringComparison.OrdinalIgnoreCase) || Sift4.GetSimilarityNormalized(subLower, cleanPub) >= 0.85)) { // Publisher registry key -> scan app subkeys try { using var pubKey = rootKey.OpenSubKey(subName); if (pubKey != null) { foreach (var appSub in pubKey.GetSubKeyNames()) { string appSubLower = appSub.ToLowerInvariant(); if (tokens.Any(t => IsWordOrExactMatch(appSubLower, t) || Sift4.GetSimilarityNormalized(appSubLower, t) >= 0.80)) { results.Add(new RegistryJunkResult { EntryName = $"{subName}\\{appSub}", Path = $@"{prefix}\{subName}\{appSub}", Confidence = ConfidenceLevel.VeryGood, Reason = "Yayıncı altındaki uygulama kayıt defteri anahtarı kalıntısı", Selected = true }); } } } } catch { } } } } } } // B6: Services strictly matched against InstallLocation folder private void ScanServicesAndDrivers(ApplicationUninstallerEntry entry, JunkScanContext context, List results) { if (string.IsNullOrWhiteSpace(entry.InstallLocation)) return; string installNorm = Path.GetFullPath(entry.InstallLocation).TrimEnd('\\') + Path.DirectorySeparatorChar; foreach (var (svcName, imagePath, displayName) in context.Services) { string cleanImagePath = imagePath.Replace("\"", "").Trim(); if (cleanImagePath.StartsWith(@"\??\")) cleanImagePath = cleanImagePath.Substring(4); // Strip executable arguments (e.g. "C:\Program Files\App\srv.exe -k runservice") int exeIdx = cleanImagePath.IndexOf(".exe", StringComparison.OrdinalIgnoreCase); if (exeIdx > 0) { cleanImagePath = cleanImagePath.Substring(0, exeIdx + 4).Trim(); } if (cleanImagePath.StartsWith(installNorm, StringComparison.OrdinalIgnoreCase)) { results.Add(new GenericJunkResult { EntryName = svcName, Path = svcName, Type = JunkType.Service, Confidence = ConfidenceLevel.VeryGood, Reason = $"Uygulamaya ait arka plan Windows Hizmeti ({displayName}) [{cleanImagePath}]", Selected = true, CustomAction = () => { try { // Backup service registry key first Directory.CreateDirectory(BackupDirectory); string file = Path.Combine(BackupDirectory, $"service_backup_{DateTime.Now:yyyyMMdd_HHmmss}_{svcName}.reg"); var psiBackup = new ProcessStartInfo("reg.exe", $"export \"HKLM\\SYSTEM\\CurrentControlSet\\Services\\{svcName}\" \"{file}\" /y") { CreateNoWindow = true, UseShellExecute = false }; using var pB = Process.Start(psiBackup); pB?.WaitForExit(3000); // Stop service var stopInfo = new ProcessStartInfo("sc.exe", $"stop \"{svcName}\"") { CreateNoWindow = true, UseShellExecute = false }; using var pStop = Process.Start(stopInfo); pStop?.WaitForExit(3000); // Delete service var delInfo = new ProcessStartInfo("sc.exe", $"delete \"{svcName}\"") { CreateNoWindow = true, UseShellExecute = false }; using var pDel = Process.Start(delInfo); pDel?.WaitForExit(3000); using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default); baseKey.DeleteSubKeyTree($@"SYSTEM\CurrentControlSet\Services\{svcName}", false); return true; } catch { return false; } } }); } } } private void ScanScheduledTasks(ApplicationUninstallerEntry entry, string cleanPub, List tokens, JunkScanContext context, List results) { string installNorm = string.IsNullOrWhiteSpace(entry.InstallLocation) ? "" : Path.GetFullPath(entry.InstallLocation).TrimEnd('\\') + Path.DirectorySeparatorChar; foreach (var (relativeTaskPath, taskFile, content) in context.ScheduledTasks) { string fileName = Path.GetFileName(taskFile).ToLowerInvariant(); bool match = false; string matchReason = ""; if (!string.IsNullOrEmpty(installNorm) && content.IndexOf(installNorm, StringComparison.OrdinalIgnoreCase) >= 0) { match = true; matchReason = "Görev eylemi kaldırılan uygulamanın kurulum klasöründeki dosyayı çalıştırıyor"; } else if (tokens.Any(t => IsWordOrExactMatch(fileName, t)) || (!string.IsNullOrEmpty(cleanPub) && relativeTaskPath.IndexOf(cleanPub, StringComparison.OrdinalIgnoreCase) >= 0)) { match = true; matchReason = "Görev adı kaldırılan yazılımla eşleşiyor"; } if (match) { results.Add(new GenericJunkResult { EntryName = Path.GetFileName(taskFile), Path = relativeTaskPath, Type = JunkType.ScheduledTask, Confidence = ConfidenceLevel.VeryGood, Reason = $"Windows Görev Zamanlayıcı (Scheduled Task) kalıntısı - {matchReason}", Selected = true, CustomAction = () => { try { var psi = new ProcessStartInfo("schtasks.exe", $"/Delete /TN \"{relativeTaskPath}\" /F") { CreateNoWindow = true, UseShellExecute = false }; using var p = Process.Start(psi); p?.WaitForExit(3000); if (File.Exists(taskFile)) File.Delete(taskFile); return true; } catch { return false; } } }); } } } private void ScanAppPaths(ApplicationUninstallerEntry entry, List tokens, List results) { string installNorm = string.IsNullOrWhiteSpace(entry.InstallLocation) ? "" : Path.GetFullPath(entry.InstallLocation).TrimEnd('\\') + Path.DirectorySeparatorChar; var roots = new[] { (Prefix: "HKLM", Key: Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths")), (Prefix: "HKCU", Key: Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths")), (Prefix: "HKLM_32", Key: RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths")) }; foreach (var (prefix, root) in roots) { if (root == null) continue; using (root) { foreach (var sub in root.GetSubKeyNames()) { try { using var appKey = root.OpenSubKey(sub); if (appKey == null) continue; string? defaultVal = appKey.GetValue(null)?.ToString(); string subLower = sub.ToLowerInvariant(); bool match = false; if (!string.IsNullOrEmpty(defaultVal) && !string.IsNullOrEmpty(installNorm) && defaultVal.StartsWith(installNorm, StringComparison.OrdinalIgnoreCase)) { match = true; } else if (tokens.Any(t => IsWordOrExactMatch(subLower, t))) { match = true; } if (match) { string path = prefix == "HKLM_32" ? $@"HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\App Paths\{sub}" : $@"{prefix}\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\{sub}"; results.Add(new RegistryJunkResult { EntryName = sub, Path = path, Confidence = ConfidenceLevel.VeryGood, Reason = "Windows Uygulama Başlatma Yolu (App Path) kalıntısı", Selected = true }); } } catch { } } } } } private void ScanStartupRunKeys(ApplicationUninstallerEntry entry, List tokens, List results) { string installNorm = string.IsNullOrWhiteSpace(entry.InstallLocation) ? "" : Path.GetFullPath(entry.InstallLocation).TrimEnd('\\') + Path.DirectorySeparatorChar; var locations = new[] { (Hive: "HKLM", Path: @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"), (Hive: "HKLM", Path: @"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"), (Hive: "HKCU", Path: @"Software\Microsoft\Windows\CurrentVersion\Run"), (Hive: "HKCU", Path: @"Software\Microsoft\Windows\CurrentVersion\RunOnce"), (Hive: "HKLM_32", Path: @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run") }; foreach (var loc in locations) { try { RegistryKey? baseKey = loc.Hive switch { "HKCU" => Registry.CurrentUser, "HKLM_32" => RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32), _ => Registry.LocalMachine }; using var key = baseKey.OpenSubKey(loc.Path); if (key == null) continue; foreach (var valName in key.GetValueNames()) { string? valData = key.GetValue(valName)?.ToString(); if (string.IsNullOrEmpty(valData)) continue; string valLower = valData.ToLowerInvariant(); bool match = false; if (!string.IsNullOrEmpty(installNorm) && valLower.Contains(installNorm.ToLowerInvariant())) match = true; else if (tokens.Any(t => IsWordOrExactMatch(valName.ToLowerInvariant(), t))) match = true; if (match) { string hiveName = loc.Hive == "HKLM_32" ? "HKLM" : loc.Hive; results.Add(new RegistryJunkResult { EntryName = valName, Path = $@"{hiveName}\{loc.Path}", ValueName = valName, Confidence = ConfidenceLevel.VeryGood, Reason = "Başlangıçta otomatik çalıştırma kayıt defteri girdisi (Run/RunOnce)", Selected = true }); } } } catch { } } } private void ScanShellExtensionsAndCom(ApplicationUninstallerEntry entry, List tokens, List results) { string installNorm = string.IsNullOrWhiteSpace(entry.InstallLocation) ? "" : Path.GetFullPath(entry.InstallLocation).TrimEnd('\\') + Path.DirectorySeparatorChar; string[] contextMenuKeys = { @"*\shellex\ContextMenuHandlers", @"Directory\shellex\ContextMenuHandlers", @"Directory\Background\shellex\ContextMenuHandlers", @"Drive\shellex\ContextMenuHandlers", @"Folder\shellex\ContextMenuHandlers" }; foreach (var cmKey in contextMenuKeys) { try { using var root = Registry.ClassesRoot.OpenSubKey(cmKey); if (root == null) continue; foreach (var sub in root.GetSubKeyNames()) { string subLower = sub.ToLowerInvariant(); if (tokens.Any(t => IsWordOrExactMatch(subLower, t))) { results.Add(new RegistryJunkResult { EntryName = sub, Path = $@"HKCR\{cmKey}\{sub}", Confidence = ConfidenceLevel.VeryGood, Reason = "Windows Gezgini sağ tık içerik menüsü eklentisi (Shell Extension)", Selected = true }); } } } catch { } } // Orphaned CLSIDs pointing into InstallLocation if (!string.IsNullOrEmpty(installNorm)) { try { using var clsidKey = Registry.ClassesRoot.OpenSubKey("CLSID"); if (clsidKey != null) { foreach (var clsid in clsidKey.GetSubKeyNames()) { try { using var sub = clsidKey.OpenSubKey(clsid); if (sub == null) continue; foreach (var serverKey in new[] { "InprocServer32", "LocalServer32" }) { using var sKey = sub.OpenSubKey(serverKey); if (sKey == null) continue; string? serverPath = sKey.GetValue(null)?.ToString(); if (!string.IsNullOrEmpty(serverPath) && serverPath.StartsWith(installNorm, StringComparison.OrdinalIgnoreCase)) { results.Add(new RegistryJunkResult { EntryName = clsid, Path = $@"HKCR\CLSID\{clsid}", Confidence = ConfidenceLevel.VeryGood, Reason = "Kaldırılan uygulamanın DLL/EXE dosyasına bağlı artık COM Nesnesi (CLSID)", Selected = true }); break; } } } catch { } } } } catch { } } } private void ScanMuiCache(ApplicationUninstallerEntry entry, List results) { string installNorm = string.IsNullOrWhiteSpace(entry.InstallLocation) ? "" : Path.GetFullPath(entry.InstallLocation).TrimEnd('\\') + Path.DirectorySeparatorChar; if (string.IsNullOrEmpty(installNorm)) return; string[] muiPaths = { @"Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache", @"Local Settings\Software\Microsoft\Windows\Shell\MuiCache" }; foreach (var mui in muiPaths) { try { using var key = Registry.CurrentUser.OpenSubKey(mui); if (key == null) continue; foreach (var val in key.GetValueNames()) { if (val.StartsWith(installNorm, StringComparison.OrdinalIgnoreCase)) { results.Add(new RegistryJunkResult { EntryName = Path.GetFileName(val), Path = $@"HKCU\{mui}", ValueName = val, Confidence = ConfidenceLevel.VeryGood, Reason = "Windows Shell MuiCache uygulama ve simge önbellek kaydı", Selected = true }); } } } catch { } } } private void ScanAppCompatFlags(ApplicationUninstallerEntry entry, List results) { string installNorm = string.IsNullOrWhiteSpace(entry.InstallLocation) ? "" : Path.GetFullPath(entry.InstallLocation).TrimEnd('\\') + Path.DirectorySeparatorChar; if (string.IsNullOrEmpty(installNorm)) return; var paths = new[] { (Hive: "HKCU", Path: @"Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"), (Hive: "HKLM", Path: @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"), (Hive: "HKCU", Path: @"Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store") }; foreach (var p in paths) { try { var baseKey = p.Hive == "HKCU" ? Registry.CurrentUser : Registry.LocalMachine; using var key = baseKey.OpenSubKey(p.Path); if (key == null) continue; foreach (var val in key.GetValueNames()) { if (val.StartsWith(installNorm, StringComparison.OrdinalIgnoreCase)) { results.Add(new RegistryJunkResult { EntryName = Path.GetFileName(val), Path = $@"{p.Hive}\{p.Path}", ValueName = val, Confidence = ConfidenceLevel.VeryGood, Reason = "Windows Uyumluluk ve AppCompat geçmiş girdisi", Selected = true }); } } } catch { } } } private void ScanFirewallRules(ApplicationUninstallerEntry entry, List results) { if (string.IsNullOrWhiteSpace(entry.InstallLocation)) return; try { using var key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules", true); if (key == null) return; string installNorm = Path.GetFullPath(entry.InstallLocation).TrimEnd('\\') + Path.DirectorySeparatorChar; foreach (var valName in key.GetValueNames()) { string? val = key.GetValue(valName)?.ToString(); if (!string.IsNullOrWhiteSpace(val) && val.IndexOf(installNorm, StringComparison.OrdinalIgnoreCase) >= 0) { results.Add(new RegistryJunkResult { EntryName = valName, Path = @"HKLM\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules", ValueName = valName, Confidence = ConfidenceLevel.VeryGood, Reason = "Uygulamaya ait geçersiz Windows Güvenlik Duvarı kuralı", Selected = true }); } } } catch { } } private void ScanPrefetch(List tokens, List results) { try { string prefetchDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Prefetch"); if (!Directory.Exists(prefetchDir)) return; foreach (var token in tokens.Take(3)) { if (token.Length < 3) continue; var pfFiles = Directory.GetFiles(prefetchDir, $"{token.ToUpperInvariant()}*.pf"); foreach (var pf in pfFiles) { results.Add(new FileSystemJunkResult { EntryName = Path.GetFileName(pf), Path = pf, IsDirectory = false, Confidence = ConfidenceLevel.VeryGood, Reason = "Windows Prefetch çalıştırma hızlandırma önbellek dosyası", Selected = true }); } } } catch { } } private void ScanWerDumps(List tokens, List results) { if (tokens.Count == 0) return; try { string crashDumps = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CrashDumps"); if (Directory.Exists(crashDumps)) { foreach (var file in Directory.GetFiles(crashDumps, "*.dmp")) { string fname = Path.GetFileName(file).ToLowerInvariant(); if (tokens.Any(t => IsWordOrExactMatch(fname, t))) { results.Add(new FileSystemJunkResult { EntryName = Path.GetFileName(file), Path = file, IsDirectory = false, Confidence = ConfidenceLevel.Good, Reason = "Windows Hata Raporlama çökme döküm dosyası (Crash Dump)", Selected = true }); } } } } catch { } } private void ScanUserAssist(List tokens, List results) { try { using var uaKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist"); if (uaKey == null) return; foreach (var sub in uaKey.GetSubKeyNames()) { using var countKey = uaKey.OpenSubKey($"{sub}\\Count", true); if (countKey == null) continue; foreach (var valName in countKey.GetValueNames()) { string decoded = Rot13(valName).ToLowerInvariant(); if (tokens.Any(t => IsWordOrExactMatch(decoded, t))) { results.Add(new RegistryJunkResult { EntryName = Rot13(valName), Path = $@"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist\{sub}\Count", ValueName = valName, Confidence = ConfidenceLevel.VeryGood, Reason = "UserAssist uygulama çalıştırma geçmişi girdisi", Selected = true }); } } } } catch { } } private void ScanShortcutsAndRecent(List tokens, JunkScanContext context, List results) { foreach (var file in context.ShortcutFiles) { string fname = Path.GetFileNameWithoutExtension(file).ToLowerInvariant(); if (tokens.Any(t => IsWordOrExactMatch(fname, t) || Sift4.GetSimilarityNormalized(fname, t) >= 0.88)) { results.Add(new FileSystemJunkResult { EntryName = Path.GetFileName(file), Path = file, IsDirectory = false, Confidence = ConfidenceLevel.VeryGood, Reason = "Masaüstü veya Başlat menüsü kısayol kalıntısı", Selected = true }); } } } private void ScanMsiOrphanedProducts(ApplicationUninstallerEntry entry, string cleanName, List results) { if (cleanName.Length < 3) return; try { using var productsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"); if (productsKey != null) { foreach (var prodCode in productsKey.GetSubKeyNames()) { try { using var installProps = productsKey.OpenSubKey($@"{prodCode}\InstallProperties"); if (installProps != null) { string? dispName = installProps.GetValue("DisplayName")?.ToString(); string? installLoc = installProps.GetValue("InstallLocation")?.ToString(); bool match = false; if (!string.IsNullOrEmpty(dispName) && dispName.Equals(cleanName, StringComparison.OrdinalIgnoreCase)) match = true; else if (!string.IsNullOrWhiteSpace(entry.InstallLocation) && !string.IsNullOrEmpty(installLoc) && Path.GetFullPath(installLoc).TrimEnd('\\').Equals(Path.GetFullPath(entry.InstallLocation).TrimEnd('\\'), StringComparison.OrdinalIgnoreCase)) match = true; if (match) { results.Add(new RegistryJunkResult { EntryName = dispName ?? prodCode, Path = $@"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\{prodCode}", Confidence = ConfidenceLevel.VeryGood, Reason = "Windows Installer (MSI) ürün kayıt kalıntısı", Selected = true }); results.Add(new RegistryJunkResult { EntryName = prodCode, Path = $@"HKCR\Installer\Products\{prodCode}", Confidence = ConfidenceLevel.VeryGood, Reason = "Windows Installer (MSI) ürün referans kalıntısı", Selected = true }); } } } catch { } } } } catch { } } private void ScanRegistryUninstallKey(ApplicationUninstallerEntry entry, List results) { if (string.IsNullOrWhiteSpace(entry.RegistryPath)) return; try { var (hive, subKey, view) = RegistryJunkResult.ParseRegistryPath(entry.RegistryPath); using var baseKey = RegistryKey.OpenBaseKey(hive, view); using var key = baseKey.OpenSubKey(subKey); if (key != null) { results.Add(new RegistryJunkResult { EntryName = Path.GetFileName(entry.RegistryPath), Path = entry.RegistryPath, Confidence = ConfidenceLevel.VeryGood, Reason = "Kaldırma sonrası sistemde kalan uninstaller kayıt defteri girdisi", Selected = true }); } } catch { } } public static int CleanJunk(IEnumerable items, string backupDir) { int cleanedCount = 0; Directory.CreateDirectory(backupDir); foreach (var item in items.Where(x => x.Selected && x.Confidence > ConfidenceLevel.Bad)) { try { if (item.Delete(backupDir)) { cleanedCount++; } } catch { } } return cleanedCount; } public static string Rot13(string input) { char[] array = input.ToCharArray(); for (int i = 0; i < array.Length; i++) { int number = (int)array[i]; if (number >= 'a' && number <= 'z') { if (number > 'm') number -= 13; else number += 13; } else if (number >= 'A' && number <= 'Z') { if (number > 'M') number -= 13; else number += 13; } array[i] = (char)number; } return new string(array); } } }