Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (88 loc) · 3.38 KB
/
Copy pathProgram.cs
File metadata and controls
100 lines (88 loc) · 3.38 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
namespace DesktopScroll;
internal static class Program
{
private const string SingleInstanceMutexName = "DesktopScroll.SingleInstance";
private const string LaunchAfterSetupArgument = "--launch-after-setup";
private const string PostInstallStartupPromptArgument = "--post-install-startup-prompt";
private const string StartupDisableArgument = "--startup-disable";
private const string StartupEnableArgument = "--startup-enable";
[STAThread]
private static void Main(string[] args)
{
try
{
ApplicationConfiguration.Initialize();
if (args.Contains(StartupEnableArgument, StringComparer.OrdinalIgnoreCase))
{
var startup = new Startup.StartupRegistrationManager();
startup.Enable();
return;
}
if (args.Contains(StartupDisableArgument, StringComparer.OrdinalIgnoreCase))
{
var startup = new Startup.StartupRegistrationManager();
startup.Disable();
return;
}
if (args.Contains(PostInstallStartupPromptArgument, StringComparer.OrdinalIgnoreCase)
&& !PromptForStartupRegistration())
{
return;
}
using var singleInstanceMutex = new Mutex(initiallyOwned: true, SingleInstanceMutexName, out var createdNew);
if (!createdNew)
{
MessageBox.Show(
"DesktopScroll is already running. Check the notification area (system tray).",
"DesktopScroll",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
Application.Run(new TrayApplicationContext());
}
catch (Exception ex)
{
WriteCrashLog(ex, args);
throw;
}
}
private static bool PromptForStartupRegistration()
{
var startupManager = new Startup.StartupRegistrationManager();
var settingsService = new SettingsService();
var settings = settingsService.Load();
var result = MessageBox.Show(
"Start DesktopScroll automatically when you sign in to Windows?",
"DesktopScroll Startup",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
settings.StartWithWindows = result == DialogResult.Yes;
if (settings.StartWithWindows)
{
startupManager.Enable();
}
else
{
startupManager.Disable();
}
settingsService.Save(settings);
return Environment.GetCommandLineArgs().Contains(LaunchAfterSetupArgument, StringComparer.OrdinalIgnoreCase);
}
private static void WriteCrashLog(Exception ex, string[] args)
{
try
{
var logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DesktopScroll", "logs");
Directory.CreateDirectory(logDir);
var logPath = Path.Combine(logDir, "startup-crash.log");
var payload = $"[{DateTime.UtcNow:O}] Startup crash\nArgs: {string.Join(" ", args)}\n{ex}\n\n";
File.AppendAllText(logPath, payload);
}
catch
{
// avoid secondary failures during crash logging
}
}
}