-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
65 lines (57 loc) · 2.44 KB
/
App.xaml.cs
File metadata and controls
65 lines (57 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Windows;
using ContextMenuEditor.Utilities;
namespace ContextMenuEditor;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Register global exception handlers as early as possible
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
DispatcherUnhandledException += OnDispatcherUnhandledException;
// Check if running with administrator privileges, relaunch if needed
if (ElevationHelper.TryRelaunchElevated(e.Args))
{
// Elevated process was launched, shut down this instance
Shutdown();
return;
}
// If we get here, we're either elevated or user declined UAC
if (!ElevationHelper.IsElevated())
{
// User declined elevation - show warning and continue (limited functionality)
MessageBox.Show(
"Context Menu Editor requires administrator privileges to modify registry entries.\n\n" +
"The application will start, but registry operations may fail.",
"Administrator Rights Required",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
// Initialize theme manager (dark mode by default)
_ = ThemeManager.Instance;
}
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
MessageBox.Show($"An unexpected error occurred and the application will now exit:\n\n{ex.Message}",
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
// Ensure process exits; this handler may be on a background thread
try { Current?.Shutdown(); } catch { /* ignore */ }
Environment.Exit(1);
}
}
private void OnDispatcherUnhandledException(object sender,
System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show($"An unexpected error occurred and the application will now exit:\n\n{e.Exception.Message}",
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
// Mark as handled to avoid default crash dialog, then shut down cleanly
e.Handled = true;
try { Shutdown(); } catch { /* ignore */ }
}
}