-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
43 lines (40 loc) · 1.65 KB
/
Copy pathProgram.cs
File metadata and controls
43 lines (40 loc) · 1.65 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
using IdleMon;
using CliWrap;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.EventLog;
using System.Reflection;
if (args is { Length: >= 1 }) {
string executablePath = Path.Combine(AppContext.BaseDirectory, "IdleMon.exe");
switch(args[0].ToLower()) {
case "/install":
await IdleService.Uninstall();
try {
_ = await Cli.Wrap("sc")
.WithArguments(new[] { "create", IdleService.SERVICE_NAME, $"binPath={executablePath}", "type=userown", "DisplayName=IdleMon", "start=auto" })
.ExecuteAsync();
Console.WriteLine("Successfully installed IdleMon service.");
} catch (Exception ex) {
Console.WriteLine($"Installation of IdleMon service failed: {ex}");
}
break;
case "/uninstall":
await IdleService.Uninstall();
break;
case "/report":
Console.WriteLine(IdleService.GetIdleReport());
break;
case "/version":
Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Version ?? null);
break;
default:
throw new NotImplementedException();
}
return;
}
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWindowsService(options => { options.ServiceName = IdleService.SERVICE_NAME; });
LoggerProviderOptions.RegisterProviderOptions<EventLogSettings, EventLogLoggerProvider>(builder.Services);
builder.Services.AddSingleton<IdleService>();
builder.Services.AddHostedService<WindowsBackgroundService>();
IHost host = builder.Build();
host.Run();