Example #1
0
 internal TaskFolder GetFolder([NotNull] string path)
 {
     if (v2Folder != null)
     {
         return(new TaskFolder(TaskService, v2Folder.GetFolder(path)));
     }
     throw new NotV1SupportedException();
 }
Example #2
0
        private void CreateSchedulerTask()
        {
            ITaskDefinition definition = scheduler.NewTask(0);

            definition.RegistrationInfo.Description =
                "This task starts the Open Hardware Monitor on Windows startup.";
            definition.Principal.RunLevel =
                TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
            definition.Settings.DisallowStartIfOnBatteries = false;
            definition.Settings.StopIfGoingOnBatteries     = false;
            definition.Settings.ExecutionTimeLimit         = "PT0S";

            ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
                TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);

            IExecAction action = (IExecAction)definition.Actions.Create(
                TASK_ACTION_TYPE.TASK_ACTION_EXEC);

            action.Path             = Application.ExecutablePath;
            action.WorkingDirectory =
                Path.GetDirectoryName(Application.ExecutablePath);

            ITaskFolder root = scheduler.GetFolder("\\");
            ITaskFolder folder;

            try {
                folder = root.GetFolder("Open Hardware Monitor");
            } catch (FileNotFoundException) {
                folder = root.CreateFolder("Open Hardware Monitor", "");
            }
            folder.RegisterTaskDefinition("Startup", definition,
                                          (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
                                          TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
        }
Example #3
0
        private void DeleteSchedulerTask()
        {
            ITaskFolder root = scheduler.GetFolder("\\");

            try {
                ITaskFolder folder = root.GetFolder("HMonZ");
                folder.DeleteTask("Startup", 0);
            } catch (IOException) { }
            try {
                root.DeleteFolder("HMonZ", 0);
            } catch (IOException) { }
        }
Example #4
0
        private void DeleteSchedulerTask()
        {
            ITaskFolder root = scheduler.GetFolder("\\");

            try {
                ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
                folder.DeleteTask("Startup", 0);
            } catch (FileNotFoundException) { }
            try {
                root.DeleteFolder("Open Hardware Monitor", 0);
            } catch (FileNotFoundException) { }
        }
        /// <summary>
        /// Creates a folder for related tasks. Not available to Task Scheduler 1.0.
        /// </summary>
        /// <param name="subFolderName">The name used to identify the folder. If "FolderName\SubFolder1\SubFolder2" is specified, the entire folder tree will be created if the folders do not exist. This parameter can be a relative path to the current <see cref="TaskFolder" /> instance. The root task folder is specified with a backslash (\). An example of a task folder path, under the root task folder, is \MyTaskFolder. The '.' character cannot be used to specify the current task folder and the '..' characters cannot be used to specify the parent task folder in the path.</param>
        /// <param name="sddlForm">The security descriptor associated with the folder.</param>
        /// <param name="exceptionOnExists">Set this value to false to avoid having an exception called if the folder already exists.</param>
        /// <returns>A <see cref="TaskFolder" /> instance that represents the new subfolder.</returns>
        /// <exception cref="System.Security.SecurityException">Security descriptor mismatch between specified credentials and credentials on existing folder by same name.</exception>
        /// <exception cref="System.ArgumentException">Invalid SDDL form.</exception>
        /// <exception cref="Microsoft.Win32.TaskScheduler.NotV1SupportedException">Not supported under Task Scheduler 1.0.</exception>
        public TaskFolder CreateFolder([NotNull] string subFolderName, string sddlForm = null, bool exceptionOnExists = true)
        {
            if (v2Folder == null)
            {
                throw new NotV1SupportedException();
            }
            ITaskFolder ifld = null;

            try { ifld = v2Folder.CreateFolder(subFolderName, sddlForm); }
            catch (COMException ce)
            {
                int serr = ce.ErrorCode & 0x0000FFFF;
                if (serr == 0xb7)                 // ERROR_ALREADY_EXISTS
                {
                    if (exceptionOnExists)
                    {
                        throw;
                    }
                    try
                    {
                        ifld = v2Folder.GetFolder(subFolderName);
                        if (ifld != null && sddlForm != null && sddlForm.Trim().Length > 0)
                        {
                            string sd = ifld.GetSecurityDescriptor((int)Task.defaultSecurityInfosSections);
                            if (string.Compare(sddlForm, sd, StringComparison.OrdinalIgnoreCase) != 0)
                            {
                                throw new SecurityException("Security descriptor mismatch between specified credentials and credentials on existing folder by same name.");
                            }
                        }
                    }
                    catch
                    {
                        if (ifld != null)
                        {
                            Marshal.ReleaseComObject(ifld);
                        }
                        throw;
                    }
                }
                else if (serr == 0x534 || serr == 0x538 || serr == 0x539 || serr == 0x53A || serr == 0x519 || serr == 0x57)
                {
                    throw new ArgumentException(@"Invalid SDDL form", nameof(sddlForm), ce);
                }
                else
                {
                    throw;
                }
            }
            return(new TaskFolder(TaskService, ifld));
        }