-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
73 lines (65 loc) · 2.21 KB
/
Program.cs
File metadata and controls
73 lines (65 loc) · 2.21 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
66
67
68
69
70
71
72
73
namespace ecomode;
using System;
using System.Security.Principal;
using System.Windows.Forms;
using System.Text;
static class Program
{
[STAThread]
static void Main()
{
// Global exception handlers (before anything else)
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += (s, e) => ShowAndLog("UI Thread Exception", e.Exception);
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
ShowAndLog("Non-UI/Background Exception", e.ExceptionObject as Exception ?? new Exception("Unknown"));
try
{
ApplicationConfiguration.Initialize();
using var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
// Wrap constructor too, in case InitializeComponent throws
MainForm form;
try
{
form = new MainForm(isAdmin);
}
catch (Exception ex)
{
ShowAndLog("Error creating MainForm (InitializeComponent?)", ex);
return;
}
Application.Run(form);
}
catch (Exception ex)
{
ShowAndLog("Fatal error in Program.Main", ex);
}
}
private static void ShowAndLog(string title, Exception ex)
{
try
{
var path = GetLogPath();
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.AppendAllText(path,
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {title}\r\n{ex}\r\n\r\n",
Encoding.UTF8);
}
catch { /* ignore logging issues */ }
try
{
MessageBox.Show($"{title}:\r\n\r\n{ex}", "EcoModeGUI — Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch { /* last resort: swallow */ }
}
private static string GetLogPath()
{
var dir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"EcoModeGUI");
return Path.Combine(dir, "error.log");
}
}