Ejemplo n.º 1
0
        private void InstallLoad(bool isInstalled, string appName)
        {
            string ExeName = Assembly.GetExecutingAssembly().Location;

            // Create or delete a task in the Task Scheduler.
            if (isInstalled)
            {
                TaskRunLevel RunLevel = PluginManager.RequireElevated ? TaskRunLevel.Highest : TaskRunLevel.LUA;
                Scheduler.AddTask(appName, ExeName, RunLevel, Logger);
            }
            else
            {
                Scheduler.RemoveTask(ExeName, out bool IsFound); // Ignore it if the task was not found.
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a task that will launch a program every time someone logs in. The program must have privilege 'AsInvoker' and NOT 'Highest'.
        /// </summary>
        /// <param name="taskName">Task name, whatever you want that can be a file name.</param>
        /// <param name="exeName">The full path to the program to launch.</param>
        /// <param name="runLevel">The program privilege when launched.</param>
        /// <returns>True if successful; otherwise, false.</returns>
        public static bool AddTask(string taskName, string exeName, TaskRunLevel runLevel)
        {
            Contract.RequireNotNull(taskName, out string TaskName);
            Contract.RequireNotNull(exeName, out string ExeName);

            try
            {
                // Remove forbidden characters since the name must not contain them.
                char[] InvalidChars = Path.GetInvalidFileNameChars();
                foreach (char InvalidChar in InvalidChars)
                {
                    TaskName = TaskName.Replace(InvalidChar, ' ');
                }

                // Create a task that launch a program when logging in.
                using TaskService Scheduler = new TaskService();
                using Trigger LogonTrigger  = Trigger.CreateTrigger(TaskTriggerType.Logon);
                using ExecAction RunAction  = (ExecAction)Microsoft.Win32.TaskScheduler.Action.CreateAction(TaskActionType.Execute);
                RunAction.Path = ExeName;

                // Try with a task name (mandatory on new versions of Windows)
                if (AddTaskToScheduler(Scheduler, TaskName, LogonTrigger, RunAction, runLevel))
                {
                    return(true);
                }

                // Try without a task name (mandatory on old versions of Windows)
                if (AddTaskToScheduler(Scheduler, null, LogonTrigger, RunAction, runLevel))
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                throw new AddTaskFailedException(e);
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds a task that will launch a program every time someone logs in. The program must have privilege 'AsInvoker' and NOT 'Highest'.
        /// </summary>
        /// <param name="taskName">Task name, whatever you want that can be a file name</param>
        /// <param name="exeName">The full path to the program to launch</param>
        /// <param name="runLevel">The program privilege when launched</param>
        /// <returns>True if successful</returns>
        public static bool AddTask(string taskName, string exeName, TaskRunLevel runLevel, IPluginLogger logger)
        {
            try
            {
                // Remove forbidden characters since the name must not contain them.
                char[] InvalidChars = Path.GetInvalidFileNameChars();
                foreach (char InvalidChar in InvalidChars)
                {
                    taskName = taskName.Replace(InvalidChar, ' ');
                }

                // Create a task that launch a program when logging in.
                TaskService Scheduler    = new TaskService();
                Trigger     LogonTrigger = Trigger.CreateTrigger(TaskTriggerType.Logon);
                ExecAction  RunAction    = Microsoft.Win32.TaskScheduler.Action.CreateAction(TaskActionType.Execute) as ExecAction;
                RunAction.Path = exeName;

                // Try with a task name (mandatory on new versions of Windows)
                if (AddTaskToScheduler(Scheduler, taskName, LogonTrigger, RunAction, runLevel, logger))
                {
                    return(true);
                }

                // Try without a task name (mandatory on old versions of Windows)
                if (AddTaskToScheduler(Scheduler, null, LogonTrigger, RunAction, runLevel, logger))
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                logger.AddLog($"(from Scheduler.AddTask) {e.Message}");
            }

            return(false);
        }
Ejemplo n.º 4
0
        private static bool AddTaskToScheduler(TaskService scheduler, string?taskName, Trigger logonTrigger, ExecAction runAction, TaskRunLevel runLevel)
        {
            try
            {
                Task task = scheduler.AddTask(taskName, logonTrigger, runAction);
                task.Definition.Principal.RunLevel = (Microsoft.Win32.TaskScheduler.TaskRunLevel)runLevel;

                Task newTask = scheduler.RootFolder.RegisterTaskDefinition(taskName, task.Definition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.None, null);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
        private static bool AddTaskToScheduler(TaskService scheduler, string taskName, Trigger logonTrigger, ExecAction runAction, TaskRunLevel runLevel, IPluginLogger logger)
        {
            try
            {
                Task task = scheduler.AddTask(taskName, logonTrigger, runAction);
                task.Definition.Principal.RunLevel = runLevel;

                Task newTask = scheduler.RootFolder.RegisterTaskDefinition(taskName, task.Definition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.None, null);
                return(true);
            }
            catch (Exception e)
            {
                logger.AddLog($"(from Scheduler.AddTaskToScheduler) {e.Message}");
            }

            return(false);
        }