250 lines
8.7 KiB
C#
250 lines
8.7 KiB
C#
using System;
|
||
using System.IO;
|
||
using Microsoft.Win32;
|
||
|
||
namespace LDUninstaller.Core.Models
|
||
{
|
||
public enum JunkType
|
||
{
|
||
RegistryKey,
|
||
RegistryValue,
|
||
Directory,
|
||
File,
|
||
Service,
|
||
ScheduledTask,
|
||
FirewallRule,
|
||
UserAssist,
|
||
Shortcut
|
||
}
|
||
|
||
public interface IJunkResult
|
||
{
|
||
string EntryName { get; }
|
||
string Path { get; }
|
||
JunkType Type { get; }
|
||
string TypeDisplay => Type switch
|
||
{
|
||
JunkType.Directory => "📁 Klasör",
|
||
JunkType.File => "📄 Dosya",
|
||
JunkType.RegistryKey => "🔑 Kayıt Anahtarı",
|
||
JunkType.RegistryValue => "🏷️ Kayıt Değeri",
|
||
JunkType.Service => "⚙️ Windows Servisi",
|
||
JunkType.ScheduledTask => "⏱️ Görev Zamanlayıcı",
|
||
JunkType.FirewallRule => "🛡️ Güvenlik Duvarı",
|
||
JunkType.UserAssist => "📊 UserAssist",
|
||
JunkType.Shortcut => "🔗 Kısayol",
|
||
_ => Type.ToString()
|
||
};
|
||
ConfidenceLevel Confidence { get; }
|
||
string ConfidenceDisplay => Confidence switch
|
||
{
|
||
ConfidenceLevel.VeryGood => "Çok Yüksek",
|
||
ConfidenceLevel.Good => "İyi",
|
||
_ => "Orta / Düşük"
|
||
};
|
||
string ConfidenceBadgeColor => Confidence switch
|
||
{
|
||
ConfidenceLevel.VeryGood => "#34D399",
|
||
ConfidenceLevel.Good => "#FBBF24",
|
||
_ => "#F87171"
|
||
};
|
||
string ConfidenceBadgeBackground => Confidence switch
|
||
{
|
||
ConfidenceLevel.VeryGood => "#064E3B",
|
||
ConfidenceLevel.Good => "#451A03",
|
||
_ => "#450A0A"
|
||
};
|
||
string Reason { get; }
|
||
bool Selected { get; set; }
|
||
bool Delete(string backupDir);
|
||
}
|
||
|
||
public class RegistryJunkResult : IJunkResult
|
||
{
|
||
public string EntryName { get; set; } = string.Empty;
|
||
public string Path { get; set; } = string.Empty; // e.g. HKLM\SOFTWARE\Vendor\AppName or HKLM_32\Software\Vendor\AppName
|
||
public string? ValueName { get; set; } // if null, entire key is deleted
|
||
public JunkType Type => ValueName == null ? JunkType.RegistryKey : JunkType.RegistryValue;
|
||
public ConfidenceLevel Confidence { get; set; } = ConfidenceLevel.Good;
|
||
public string Reason { get; set; } = string.Empty;
|
||
public bool Selected { get; set; } = true;
|
||
|
||
public bool Delete(string backupDir)
|
||
{
|
||
try
|
||
{
|
||
BackupToReg(backupDir);
|
||
var (hive, subKey, view) = ParseRegistryPath(Path);
|
||
using var baseKey = RegistryKey.OpenBaseKey(hive, view);
|
||
|
||
if (ValueName == null)
|
||
{
|
||
// Check if key exists first
|
||
using var check = baseKey.OpenSubKey(subKey);
|
||
if (check == null) return false;
|
||
|
||
baseKey.DeleteSubKeyTree(subKey, false);
|
||
}
|
||
else
|
||
{
|
||
using var opened = baseKey.OpenSubKey(subKey, true);
|
||
if (opened == null) return false;
|
||
opened.DeleteValue(ValueName, false);
|
||
}
|
||
return true;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private void BackupToReg(string backupDir)
|
||
{
|
||
try
|
||
{
|
||
Directory.CreateDirectory(backupDir);
|
||
string exportPath = Path;
|
||
string extraArgs = "";
|
||
|
||
if (exportPath.StartsWith("HKLM_32\\", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
exportPath = "HKLM\\" + exportPath.Substring("HKLM_32\\".Length);
|
||
extraArgs = " /reg:32";
|
||
}
|
||
else if (exportPath.Contains("WOW6432Node", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
extraArgs = " /reg:32";
|
||
}
|
||
|
||
string sanitized = System.Text.RegularExpressions.Regex.Replace(Path, @"[\\/:\*\?""<>\|]", "_");
|
||
string file = System.IO.Path.Combine(backupDir, $"reg_backup_{DateTime.Now:yyyyMMdd_HHmmss}_{sanitized}.reg");
|
||
var psi = new System.Diagnostics.ProcessStartInfo("reg.exe", $"export \"{exportPath}\" \"{file}\" /y{extraArgs}")
|
||
{
|
||
CreateNoWindow = true,
|
||
UseShellExecute = false
|
||
};
|
||
using var p = System.Diagnostics.Process.Start(psi);
|
||
p?.WaitForExit(3000);
|
||
}
|
||
catch
|
||
{
|
||
// Non-fatal if backup tool fails
|
||
}
|
||
}
|
||
|
||
public static (RegistryHive hive, string subKey, RegistryView view) ParseRegistryPath(string fullPath)
|
||
{
|
||
RegistryView view = RegistryView.Default;
|
||
string working = fullPath;
|
||
|
||
if (working.StartsWith("HKLM_32\\", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
view = RegistryView.Registry32;
|
||
working = "HKLM\\" + working.Substring("HKLM_32\\".Length);
|
||
}
|
||
else if (working.Contains("WOW6432Node", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
view = RegistryView.Registry32;
|
||
}
|
||
|
||
int firstSlash = working.IndexOf('\\');
|
||
string hiveStr = firstSlash > 0 ? working[..firstSlash] : working;
|
||
string sub = firstSlash > 0 ? working[(firstSlash + 1)..] : "";
|
||
|
||
RegistryHive hive = hiveStr.ToUpperInvariant() switch
|
||
{
|
||
"HKLM" or "HKEY_LOCAL_MACHINE" => RegistryHive.LocalMachine,
|
||
"HKCU" or "HKEY_CURRENT_USER" => RegistryHive.CurrentUser,
|
||
"HKCR" or "HKEY_CLASSES_ROOT" => RegistryHive.ClassesRoot,
|
||
"HKU" or "HKEY_USERS" => RegistryHive.Users,
|
||
_ => RegistryHive.LocalMachine
|
||
};
|
||
|
||
return (hive, sub, view);
|
||
}
|
||
}
|
||
|
||
public class FileSystemJunkResult : IJunkResult
|
||
{
|
||
public string EntryName { get; set; } = string.Empty;
|
||
public string Path { get; set; } = string.Empty;
|
||
public bool IsDirectory { get; set; }
|
||
public JunkType Type => IsDirectory ? JunkType.Directory : JunkType.File;
|
||
public ConfidenceLevel Confidence { get; set; } = ConfidenceLevel.Good;
|
||
public string Reason { get; set; } = string.Empty;
|
||
public bool Selected { get; set; } = true;
|
||
|
||
public bool Delete(string backupDir)
|
||
{
|
||
try
|
||
{
|
||
// B32: If target does not exist, return false
|
||
if (IsDirectory && !Directory.Exists(Path))
|
||
return false;
|
||
if (!IsDirectory && !File.Exists(Path))
|
||
return false;
|
||
|
||
// B2: Always use Windows Shell API to send items safely to the Recycle Bin!
|
||
if (Native.Win32.SendToRecycleBin(Path))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
// Fallback to direct delete if Shell operation was unsupported on volume
|
||
if (IsDirectory)
|
||
{
|
||
if (Directory.Exists(Path))
|
||
{
|
||
Directory.Delete(Path, true);
|
||
return true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (File.Exists(Path))
|
||
{
|
||
try
|
||
{
|
||
File.Delete(Path);
|
||
return true;
|
||
}
|
||
catch
|
||
{
|
||
// If file is locked, queue delete on reboot
|
||
return Native.Win32.ScheduleDeleteOnReboot(Path);
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
public class GenericJunkResult : IJunkResult
|
||
{
|
||
public string EntryName { get; set; } = string.Empty;
|
||
public string Path { get; set; } = string.Empty;
|
||
public JunkType Type { get; set; }
|
||
public ConfidenceLevel Confidence { get; set; } = ConfidenceLevel.Good;
|
||
public string Reason { get; set; } = string.Empty;
|
||
public bool Selected { get; set; } = true;
|
||
public Func<bool>? CustomAction { get; set; }
|
||
|
||
public bool Delete(string backupDir)
|
||
{
|
||
try
|
||
{
|
||
return CustomAction?.Invoke() ?? true;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|