-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (95 loc) · 4.02 KB
/
Program.cs
File metadata and controls
105 lines (95 loc) · 4.02 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
namespace WPUService;
internal static class Program
{
private const string AppTitle = "Workstation Presence Utility";
[STAThread]
private static int Main(string[] args)
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += (_, e) => HandleCrash("UI thread", e.Exception);
AppDomain.CurrentDomain.UnhandledException += (_, e) => HandleCrash("background thread", e.ExceptionObject as Exception);
try { Installer.EnsureAumidRegistration(); } catch { }
try { NativeMethods.SetCurrentProcessExplicitAppUserModelID(AppIdentity.Aumid); } catch { }
ApplicationConfiguration.Initialize();
if (args.Length > 0)
{
var cmd = args[0].TrimStart('/', '-').ToLowerInvariant();
if (cmd is "install")
{
var ok = Installer.Install();
MessageBox.Show(
ok ? $"{AppTitle} installed.\n\nIt is now running in your system tray and will start automatically with Windows."
: $"{AppTitle} could not be installed.",
AppTitle,
MessageBoxButtons.OK,
ok ? MessageBoxIcon.Information : MessageBoxIcon.Error);
return ok ? 0 : 1;
}
if (cmd is "uninstall")
{
var ok = Installer.Uninstall();
MessageBox.Show(
ok ? $"{AppTitle} has been removed from this computer."
: $"{AppTitle} could not be fully removed.",
AppTitle,
MessageBoxButtons.OK,
ok ? MessageBoxIcon.Information : MessageBoxIcon.Error);
return ok ? 0 : 1;
}
}
if (!Installer.IsRunningFromInstallLocation())
{
var choice = MessageBox.Show(
$"Install {AppTitle} to your user profile so it starts with Windows?" +
"\n\nYes – Install and start" +
"\nNo – Run once from the current location" +
"\nCancel – Exit",
AppTitle,
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
if (choice == DialogResult.Cancel) return 0;
if (choice == DialogResult.Yes)
{
var ok = Installer.Install();
MessageBox.Show(
ok ? $"{AppTitle} installed and started in your system tray."
: $"{AppTitle} could not be installed.",
AppTitle,
MessageBoxButtons.OK,
ok ? MessageBoxIcon.Information : MessageBoxIcon.Error);
return ok ? 0 : 1;
}
}
using var mutex = new Mutex(initiallyOwned: true, name: @"Global\WPUService.SingleInstance", out bool createdNew);
if (!createdNew) return 0;
Application.Run(new TrayContext());
return 0;
}
internal static void LogException(string source, Exception? ex)
{
var details = ex?.ToString() ?? "(no exception object)";
try
{
var dir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"WPUService");
Directory.CreateDirectory(dir);
var line = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {source}: {details}{Environment.NewLine}{Environment.NewLine}";
File.AppendAllText(Path.Combine(dir, "error.log"), line);
}
catch { }
}
private static void HandleCrash(string source, Exception? ex)
{
LogException(source, ex);
try
{
MessageBox.Show(
$"{AppTitle} hit an unexpected error and the details were written to error.log in your WPUService AppData folder.\n\n{ex?.GetType().Name}: {ex?.Message}",
AppTitle,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch { }
}
}