|
22 | 22 | using System.Configuration; |
23 | 23 | using System.Xml; |
24 | 24 | using NLog; |
| 25 | +using Microsoft.Win32.TaskScheduler; |
25 | 26 |
|
26 | 27 | namespace AssistantComputerControl { |
27 | 28 | class MainProgram { |
28 | 29 | public const string softwareVersion = "1.4.2", |
29 | | - releaseDate = "2020-03-14 21:25:00", //YYYY-MM-DD H:i:s - otherwise it gives an error |
| 30 | + releaseDate = "2020-08-02 21:25:00", //YYYY-MM-DD H:i:s - otherwise it gives an error |
30 | 31 | appName = "AssistantComputerControl", |
31 | 32 |
|
32 | | - //sentryToken = "super_secret"; |
33 | | - sentryToken = "https://[email protected]/1287269"; //Remove on git push |
| 33 | + sentryToken = "super_secret"; |
34 | 34 |
|
35 | 35 | static public bool debug = true, |
36 | 36 | unmuteVolumeChange = true, |
@@ -340,6 +340,7 @@ void ActualMain() { |
340 | 340 |
|
341 | 341 | //Action mods implementation |
342 | 342 | ActionMods.CheckMods(); |
| 343 | + TaskSchedulerSetup(); |
343 | 344 |
|
344 | 345 | SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch); //On wake up from sleep |
345 | 346 | Application.Run(); |
@@ -397,6 +398,31 @@ static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e) |
397 | 398 | } |
398 | 399 | } |
399 | 400 |
|
| 401 | + public static void TaskSchedulerSetup () { |
| 402 | + //Create "Task Scheduler" service; cleanup ACC on startup, log on, workstation unlock |
| 403 | + try { |
| 404 | + using (TaskService ts = new TaskService()) { |
| 405 | + var ps1File = Path.Combine(MainProgram.currentLocation, "ExtraCleanupper.ps1"); |
| 406 | + |
| 407 | + TaskDefinition td = ts.NewTask(); |
| 408 | + td.RegistrationInfo.Author = "Albert MN. | AssistantComputerControl"; |
| 409 | + td.RegistrationInfo.Description = "AssistantComputerControl cleanup - clears the action folder to prevent the same action being executed twice"; |
| 410 | + td.Triggers.Add(new BootTrigger()); |
| 411 | + td.Triggers.Add(new LogonTrigger()); |
| 412 | + td.Triggers.Add(new SessionStateChangeTrigger { StateChange = TaskSessionStateChangeType.SessionUnlock }); |
| 413 | + td.Actions.Add(new ExecAction("powershell.exe", $"-WindowStyle Hidden -file \"{ps1File}\" \"{Path.Combine(MainProgram.CheckPath(), "*")}\" \"*.{Properties.Settings.Default.ActionFileExtension}\"", null)); |
| 414 | + |
| 415 | + // Register the task in the root folder |
| 416 | + ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl cleanup", td); |
| 417 | + |
| 418 | + // Remove the task we just created |
| 419 | + //ts.RootFolder.DeleteTask("Test"); |
| 420 | + } |
| 421 | + } catch { |
| 422 | + DoDebug("Failed to create / update Task Scheduler service"); |
| 423 | + } |
| 424 | + } |
| 425 | + |
400 | 426 | private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args) { |
401 | 427 | Exception e = (Exception)args.ExceptionObject; |
402 | 428 | string errorLogLoc = Path.Combine(dataFolderLocation, "error_log.txt"); |
@@ -505,6 +531,7 @@ private static bool CreateLogFile() { |
505 | 531 | public static void SetupListener() { |
506 | 532 | watcher.Path = CheckPath(); |
507 | 533 | watcher.Filter = "*." + Properties.Settings.Default.ActionFileExtension; |
| 534 | + TaskSchedulerSetup(); |
508 | 535 | DoDebug("Listener modified"); |
509 | 536 | } |
510 | 537 |
|
|
0 commit comments