11/*
22 * AssistantComputerControl
33 * Made by Albert MN.
4- * Updated: v1.4.2, 12-12-2020
5- * Updated: v1.4.2, 18-04-2020
4+ * Updated: v1.4.3, 20-04-2021
65 *
76 * Use:
87 * - Main class. Starts everything.
2726
2827namespace AssistantComputerControl {
2928 class MainProgram {
30- public const string softwareVersion = "1.4.2 " ,
31- releaseDate = "2020-12-12 14:55 :00" , //YYYY-MM-DD H:i:s - otherwise it gives an error
29+ public const string softwareVersion = "1.4.3 " ,
30+ releaseDate = "2021-04-20 23:18 :00" , //YYYY-MM-DD H:i:s - otherwise it gives an error
3231 appName = "AssistantComputerControl" ,
3332
3433 sentryToken = "super_secret" ;
@@ -73,6 +72,8 @@ static void Main(string[] args) {
7372 Console . WriteLine ( "Log location; " + logFilePath ) ;
7473 CheckSettings ( ) ;
7574
75+
76+
7677 var config = new NLog . Config . LoggingConfiguration ( ) ;
7778 var logfile = new NLog . Targets . FileTarget ( "logfile" ) { FileName = logFilePath } ;
7879 var logconsole = new NLog . Targets . ConsoleTarget ( "logconsole" ) ;
@@ -278,6 +279,8 @@ void ActualMain() {
278279
279280 RegistryKey key = Registry . CurrentUser . OpenSubKey ( "Software" , true ) ;
280281 if ( Registry . GetValue ( key . Name + @"\AssistantComputerControl" , "FirstTime" , null ) == null ) {
282+ SetStartup ( true ) ;
283+
281284 key . CreateSubKey ( "AssistantComputerControl" ) ;
282285 key = key . OpenSubKey ( "AssistantComputerControl" , true ) ;
283286 key . SetValue ( "FirstTime" , false ) ;
@@ -399,66 +402,6 @@ static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
399402 }
400403 }
401404
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-
462405 public static void TaskSchedulerSetup ( ) {
463406 //Create "Task Scheduler" service; cleanup ACC on startup, log on, workstation unlock
464407 try {
@@ -474,39 +417,12 @@ public static void TaskSchedulerSetup () {
474417 td . Triggers . Add ( new SessionStateChangeTrigger { StateChange = TaskSessionStateChangeType . SessionUnlock } ) ;
475418 td . Actions . Add ( new ExecAction ( "powershell.exe" , $ "-WindowStyle Hidden -file \" { ps1File } \" \" { Path . Combine ( MainProgram . CheckPath ( ) , "*" ) } \" \" *.{ Properties . Settings . Default . ActionFileExtension } \" ", null ) ) ;
476419
477- // Register the task in the root folder
420+ //Register the task in the root folder
478421 ts . RootFolder . RegisterTaskDefinition ( @"AssistantComputerControl cleanup" , td ) ;
479-
480- // Remove the task we just created
481- //ts.RootFolder.DeleteTask("Test");
482422 }
483423 } catch {
484424 DoDebug ( "Failed to create / update Task Scheduler service" ) ;
485425 }
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- }
510426 }
511427
512428 private static void CurrentDomain_UnhandledException ( object sender , UnhandledExceptionEventArgs args ) {
@@ -711,21 +627,39 @@ public static void Exit() {
711627
712628 public static void SetStartup ( bool status , bool setThroughSoftware = false ) {
713629 try {
714- bool res = false ;
715630 if ( status ) {
716- res = UpdateUserTaskInScheduler ( "Disable" ) ;
631+ //Create "Task Scheduler" service; run ACC on startup & log on, added by Shelby Marvell
632+ try {
633+ using ( TaskService ts = new TaskService ( ) ) {
634+ var ps1File = Path . Combine ( MainProgram . currentLocation , "ExtraCleanupper.ps1" ) ;
635+
636+ TaskDefinition td = ts . NewTask ( ) ;
637+ td . Principal . LogonType = TaskLogonType . S4U ;
638+ td . Principal . RunLevel = TaskRunLevel . Highest ;
639+ td . RegistrationInfo . Author = "Albert MN. | AssistantComputerControl" ;
640+ td . RegistrationInfo . Description = "AssistantComputerControl startup - Runs ACC on reboot/login" ;
641+ td . Triggers . Add ( new BootTrigger ( ) ) ;
642+ td . Triggers . Add ( new LogonTrigger ( ) ) ;
643+ td . Actions . Add ( new ExecAction ( Application . ExecutablePath , null , null ) ) ;
644+
645+ //Register the task in the root folder
646+ ts . RootFolder . RegisterTaskDefinition ( @"AssistantComputerControl startup" , td ) ;
647+ }
648+ } catch {
649+ DoDebug ( "Failed to create / update Task Scheduler startup service" ) ;
650+ }
717651 } else {
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" ) ;
652+ //Create "Task Scheduler" service; run ACC on startup & log on, added by Shelby Marvell
653+ try {
654+ using ( TaskService ts = new TaskService ( ) ) {
655+ // Register the task in the root folder
656+ ts . RootFolder . DeleteTask ( @"AssistantComputerControl startup" ) ;
657+ }
658+ } catch {
659+ DoDebug ( "Failed to create / update Task Scheduler startup service" ) ;
727660 }
728661 }
662+
729663 } catch {
730664 DoDebug ( "Failed to start ACC with Windows" ) ;
731665 if ( ! setThroughSoftware ) {
@@ -736,18 +670,14 @@ public static void SetStartup(bool status, bool setThroughSoftware = false) {
736670
737671 public static bool ACCStartsWithWindows ( ) {
738672 try {
739- RegistryKey rk = Registry . CurrentUser . OpenSubKey ( "SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Run" , true ) ;
740-
741- var theVal = rk . GetValue ( appName ) ;
742- if ( theVal != null ) {
743- return true ;
744- } else {
745- return false ;
673+ using ( TaskService ts = new TaskService ( ) ) {
674+ return ts . GetTask ( @"AssistantComputerControl startup" ) != null ;
746675 }
747- } catch {
748- DoDebug ( "Failed to get ACC start with windows state" ) ;
749- return false ;
676+ } catch ( Exception e ) {
677+ DoDebug ( "Something went wrong with TaskService, checking if ACC starts with Windows; " + e . Message ) ;
750678 }
679+
680+ return false ;
751681 }
752682
753683 public static bool HasInternet ( ) {
0 commit comments