193 lines
6.5 KiB
C#
193 lines
6.5 KiB
C#
using System;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Runtime.InteropServices;
|
||
using System.Threading;
|
||
using System.Windows;
|
||
|
||
namespace LDUninstaller.UI
|
||
{
|
||
public partial class App : Application
|
||
{
|
||
[DllImport("shell32.dll", SetLastError = true)]
|
||
private static extern int SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string AppID);
|
||
|
||
[DllImport("user32.dll")]
|
||
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
||
|
||
[DllImport("user32.dll")]
|
||
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
||
|
||
private const int SW_RESTORE = 9;
|
||
private static Mutex? _instanceMutex;
|
||
private static readonly object _logLock = new();
|
||
|
||
public static string LogFilePath { get; } = Path.Combine(
|
||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||
"LDUninstaller",
|
||
"logs",
|
||
"trace.log");
|
||
|
||
protected override void OnStartup(StartupEventArgs e)
|
||
{
|
||
// Shell Context Menu: Direct Force Shredder & Unlocker Mode
|
||
string? forcePath = GetForcePathParam(e.Args);
|
||
if (!string.IsNullOrEmpty(forcePath))
|
||
{
|
||
InitLogging();
|
||
Log($"Direct force-path invoked: {forcePath}");
|
||
var forceWin = new Dialogs.ForceDeleteWindow(forcePath);
|
||
forceWin.Show();
|
||
return;
|
||
}
|
||
|
||
// B24: Global single-instance Mutex (Main Application)
|
||
bool isFirstInstance;
|
||
try
|
||
{
|
||
_instanceMutex = new Mutex(true, @"Global\LDUninstaller_SingleInstance_Mutex", out isFirstInstance);
|
||
}
|
||
catch
|
||
{
|
||
isFirstInstance = true;
|
||
}
|
||
|
||
if (!isFirstInstance)
|
||
{
|
||
// Activate running instance
|
||
try
|
||
{
|
||
var current = Process.GetCurrentProcess();
|
||
foreach (var proc in Process.GetProcessesByName(current.ProcessName))
|
||
{
|
||
if (proc.Id != current.Id && proc.MainWindowHandle != IntPtr.Zero)
|
||
{
|
||
ShowWindow(proc.MainWindowHandle, SW_RESTORE);
|
||
SetForegroundWindow(proc.MainWindowHandle);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
|
||
Shutdown();
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
// Unify taskbar grouping with shortcuts and ensure correct icon binding
|
||
SetCurrentProcessExplicitAppUserModelID("LDSoftware.LDUninstaller");
|
||
}
|
||
catch { }
|
||
|
||
base.OnStartup(e);
|
||
|
||
// B30: %LocalAppData%\LDUninstaller\logs\trace.log with rolling size limit
|
||
InitLogging();
|
||
|
||
AppDomain.CurrentDomain.ProcessExit += (s, args) =>
|
||
{
|
||
Log("Process exit triggered.");
|
||
};
|
||
|
||
AppDomain.CurrentDomain.UnhandledException += (s, args) =>
|
||
{
|
||
Log($"UNHANDLED: {args.ExceptionObject}");
|
||
};
|
||
|
||
DispatcherUnhandledException += (s, args) =>
|
||
{
|
||
Log($"DISPATCHER UNHANDLED: {args.Exception}");
|
||
MessageBox.Show(
|
||
$"Arayüz Hatası:\n{args.Exception.Message}\n\nDetaylar log dosyasına kaydedildi:\n{LogFilePath}",
|
||
"Hata - LD Uninstaller",
|
||
MessageBoxButton.OK,
|
||
MessageBoxImage.Error);
|
||
args.Handled = true;
|
||
};
|
||
}
|
||
|
||
private static void InitLogging()
|
||
{
|
||
try
|
||
{
|
||
string? dir = Path.GetDirectoryName(LogFilePath);
|
||
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
||
{
|
||
Directory.CreateDirectory(dir);
|
||
}
|
||
|
||
// Roll if > 5 MB
|
||
if (File.Exists(LogFilePath))
|
||
{
|
||
var fi = new FileInfo(LogFilePath);
|
||
if (fi.Length > 5 * 1024 * 1024)
|
||
{
|
||
string oldLog = Path.Combine(dir!, "trace.old.log");
|
||
if (File.Exists(oldLog)) File.Delete(oldLog);
|
||
File.Move(LogFilePath, oldLog);
|
||
}
|
||
}
|
||
|
||
Log($"=== LD Uninstaller Başlatıldı - {DateTime.Now:yyyy-MM-dd HH:mm:ss} ===");
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
public static void Log(string message)
|
||
{
|
||
try
|
||
{
|
||
lock (_logLock)
|
||
{
|
||
string? dir = Path.GetDirectoryName(LogFilePath);
|
||
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
||
{
|
||
Directory.CreateDirectory(dir);
|
||
}
|
||
|
||
string line = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}{Environment.NewLine}";
|
||
File.AppendAllText(LogFilePath, line);
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
private static string? GetForcePathParam(string[] args)
|
||
{
|
||
if (args == null || args.Length == 0) return null;
|
||
for (int i = 0; i < args.Length; i++)
|
||
{
|
||
string arg = args[i];
|
||
if (arg.Equals("--force-path", StringComparison.OrdinalIgnoreCase) ||
|
||
arg.Equals("/force-path", StringComparison.OrdinalIgnoreCase) ||
|
||
arg.Equals("-force-path", StringComparison.OrdinalIgnoreCase) ||
|
||
arg.Equals("/target", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
if (i + 1 < args.Length)
|
||
return args[i + 1].Trim('\"');
|
||
}
|
||
else if (arg.StartsWith("--force-path=", StringComparison.OrdinalIgnoreCase) ||
|
||
arg.StartsWith("/force-path=", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
int eq = arg.IndexOf('=');
|
||
return arg[(eq + 1)..].Trim('\"');
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
protected override void OnExit(ExitEventArgs e)
|
||
{
|
||
try
|
||
{
|
||
_instanceMutex?.ReleaseMutex();
|
||
_instanceMutex?.Dispose();
|
||
}
|
||
catch { }
|
||
|
||
base.OnExit(e);
|
||
}
|
||
}
|
||
}
|