@@ -399,6 +399,66 @@ static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
399399 }
400400 }
401401
402+ private static bool UpdateUserTaskInScheduler ( string action )
403+ {
404+ try
405+ {
406+ ProcessStartInfo startInfo = new ProcessStartInfo ( ) ;
407+ startInfo . FileName = "cmd.exe" ;
408+ startInfo . Arguments = "/C schtasks /query /TN \" AssistantComputerControl startup\" " ; //Check if task exists
409+ startInfo . RedirectStandardOutput = true ;
410+ startInfo . UseShellExecute = false ;
411+ startInfo . CreateNoWindow = true ;
412+ startInfo . WindowStyle = ProcessWindowStyle . Hidden ;
413+ if ( System . Environment . OSVersion . Version . Major < 6 )
414+ {
415+ startInfo . Verb = "runas" ;
416+ }
417+ using ( Process process = Process . Start ( startInfo ) )
418+ {
419+ // Read in all the text from the process with the StreamReader.
420+ using ( StreamReader reader = process . StandardOutput )
421+ {
422+ string stdout = reader . ReadToEnd ( ) ;
423+ if ( stdout . Contains ( "<<TaskName>>" ) ) //If task exists
424+ {
425+ startInfo . RedirectStandardOutput = false ;
426+ startInfo . UseShellExecute = true ;
427+ switch ( action )
428+ {
429+ case "Enable" :
430+ startInfo . Arguments = "/C schtasks /Change /TN \" AssistantComputerControl startup\" /Enable" ;
431+ break ;
432+
433+ case "Disable" :
434+ startInfo . Arguments = "/C schtasks /Change /TN \" AssistantComputerControl startup\" /Disable" ;
435+ break ;
436+
437+ case "Run" :
438+ startInfo . Arguments = "/C schtasks /RUN /TN \" AssistantComputerControl startup\" " ;
439+ break ;
440+ }
441+ Process . Start ( startInfo ) . WaitForExit ( ) ;
442+ }
443+ else
444+ {
445+ return false ;
446+ }
447+ stdout = null ;
448+ reader . Close ( ) ;
449+ reader . Dispose ( ) ;
450+ }
451+ }
452+ startInfo = null ;
453+ return true ;
454+ }
455+ catch ( Exception ex )
456+ {
457+ MessageBox . Show ( ex . Message ) ;
458+ return false ;
459+ }
460+ }
461+
402462 public static void TaskSchedulerSetup ( ) {
403463 //Create "Task Scheduler" service; cleanup ACC on startup, log on, workstation unlock
404464 try {
@@ -423,6 +483,30 @@ public static void TaskSchedulerSetup () {
423483 } catch {
424484 DoDebug ( "Failed to create / update Task Scheduler service" ) ;
425485 }
486+ //Create "Task Scheduler" service; run ACC on startup & log on, added by Shelby Marvell
487+ try
488+ {
489+ using ( TaskService ts = new TaskService ( ) )
490+ {
491+ var ps1File = Path . Combine ( MainProgram . currentLocation , "ExtraCleanupper.ps1" ) ;
492+
493+ TaskDefinition td = ts . NewTask ( ) ;
494+ td . Principal . LogonType = TaskLogonType . S4U ;
495+ td . Principal . RunLevel = TaskRunLevel . Highest ;
496+ td . RegistrationInfo . Author = "Albert MN. | AssistantComputerControl" ;
497+ td . RegistrationInfo . Description = "AssistantComputerControl startup - Runs ACC on reboot/login" ;
498+ td . Triggers . Add ( new BootTrigger ( ) ) ;
499+ td . Triggers . Add ( new LogonTrigger ( ) ) ;
500+ td . Actions . Add ( new ExecAction ( Application . ExecutablePath , null , null ) ) ;
501+
502+ // Register the task in the root folder
503+ ts . RootFolder . RegisterTaskDefinition ( @"AssistantComputerControl startup" , td ) ;
504+ }
505+ }
506+ catch
507+ {
508+ DoDebug ( "Failed to create / update Task Scheduler startup service" ) ;
509+ }
426510 }
427511
428512 private static void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs args ) {
@@ -627,14 +711,20 @@ public static void Exit() {
627711
628712 public static void SetStartup ( bool status , bool setThroughSoftware = false ) {
629713 try {
630- RegistryKey rk = Registry . CurrentUser . OpenSubKey ( "SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Run" , true ) ;
631-
714+ bool res = false ;
632715 if ( status ) {
633- rk . SetValue ( appName , Application . ExecutablePath ) ;
634- DoDebug ( "ACC now starts with Windows" ) ;
716+ res = UpdateUserTaskInScheduler ( "Disable" ) ;
635717 } else {
636- rk . DeleteValue ( appName , false ) ;
637- DoDebug ( "ACC no longer starts with Windows" ) ;
718+ res = UpdateUserTaskInScheduler ( "Enable" ) ;
719+ }
720+ while ( ! res ) {
721+ // Some error occurred. Try recreating the task.
722+ TaskSchedulerSetup ( ) ;
723+ if ( status ) {
724+ res = UpdateUserTaskInScheduler ( "Disable" ) ;
725+ } else {
726+ res = UpdateUserTaskInScheduler ( "Enable" ) ;
727+ }
638728 }
639729 } catch {
640730 DoDebug ( "Failed to start ACC with Windows" ) ;
0 commit comments