Example #1
0
        public int Uninstall()
        {
            int result = 0;

            Console.WriteLine("Start Uninstall..");
            this.withThrowExceptionIfPermissionDeneed();

            // TODO: Registry restore

            // delete shortcut
            string          exeFullPathFileName = Process.GetCurrentProcess().MainModule.FileName;
            ShortcutCreator shortcut            = new ShortcutCreator(exeFullPathFileName, "-s", ShortcutCreator.TargetSpecialFolder.CommonPrograms);

            shortcut.delete();

            // delete task
            if (isTargetTask())
            {
                using (TaskService taskService = new TaskService())
                {
                    taskService.RootFolder.DeleteTask(D_PREVENT_REBOOT_TASK);
                }
            }

            return(result);
        }
Example #2
0
        public int Install()
        {
            int result = 0;

            Console.WriteLine("Start Install..");
            this.withThrowExceptionIfPermissionDeneed();

            if (isTargetTask())
            {
                throw new InvalidOperationException("Task has already been setup. Please uninstall first.");
            }

            // TODO: Registry save (Only on first run)

            // create shortcut
            //string path = AppDomain.CurrentDomain.BaseDirectory;
            string exeFullPathFileName = Process.GetCurrentProcess().MainModule.FileName;
            //Console.WriteLine($"path       : {path}");
            //Console.WriteLine($"name       : {exeFullPathFileName}");

            // create shortcut
            ShortcutCreator shortcut = new ShortcutCreator(exeFullPathFileName, "-s", ShortcutCreator.TargetSpecialFolder.CommonPrograms);

            shortcut.AppUserModelID = Program.generateDefaultAppUserModelID();
            shortcut.create();

            // create task
            using (TaskService taskService = new TaskService())
            {
                // regist task scheduler
                TimeSpan span = TimeSpan.FromHours(1);
                Task     task = taskService.Execute(exeFullPathFileName).WithArguments("-s").AtLogon().RepeatingEvery(span).AsTask(D_PREVENT_REBOOT_TASK);

                TaskDefinition definition = task.Definition;

                TaskPrincipal principal = definition.Principal;
                principal.RunLevel = TaskRunLevel.Highest;
                principal.UserId   = "SYSTEM";

                TaskRegistrationInfo info = definition.RegistrationInfo;
                info.Author      = "PreventReboot";
                info.Description = "This program prevents reboot during login by updating the ActiveTime of Windows10 on a regular basis.";

                TaskSettings settings = definition.Settings;
                settings.DisallowStartIfOnBatteries = false;
                settings.StopIfGoingOnBatteries     = false;
                settings.RunOnlyIfIdle      = false;
                settings.Hidden             = true;
                settings.Enabled            = true;
                settings.StartWhenAvailable = true;

                IdleSettings idle = settings.IdleSettings;
                idle.RestartOnIdle = false;
                idle.StopOnIdleEnd = false;

                task.RegisterChanges();
            }

            return(result);
        }