private void attachTaskToThisEventToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (selectedIndex != -1)
     {
         var taskEvent = (TaskEvent)vcache[selectedIndex].Tag;
         if (taskEvent != null)
         {
             var td      = task.TaskService.NewTask();
             var eventId = taskEvent.EventId;
             td.Triggers.Add(new EventTrigger("Microsoft-Windows-TaskScheduler/Operational", "TaskScheduler", eventId));
             td.Actions.Add(new ExecAction());
             using (var wiz = new TaskSchedulerWizard(task.TaskService, td, true))
             {
                 wiz.AvailableTriggers = TaskSchedulerWizard.AvailableWizardTriggers.Event;
                 wiz.AvailableActions  = TaskSchedulerWizard.AvailableWizardActions.Execute;
                 wiz.AvailablePages    = TaskSchedulerWizard.AvailableWizardPages.IntroPage | TaskSchedulerWizard.AvailableWizardPages.SecurityPage | TaskSchedulerWizard.AvailableWizardPages.SummaryPage | TaskSchedulerWizard.AvailableWizardPages.TriggerEditPage | TaskSchedulerWizard.AvailableWizardPages.ActionEditPage;
                 wiz.TaskName          = string.Format("Microsoft-Windows-TaskScheduler_Operational_Microsoft-Windows-TaskScheduler_{0}", eventId);
                 wiz.ShowDialog(this);
             }
         }
     }
 }
 private void attachTaskToThisEventToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (selectedIndex != -1)
     {
         var taskEvent = (TaskEvent)vcache[selectedIndex].Tag;
         if (taskEvent != null)
         {
             var td      = task.TaskService.NewTask();
             var eventId = taskEvent.EventId;
             td.Triggers.Add(new EventTrigger("Microsoft-Windows-TaskScheduler/Operational", "TaskScheduler", eventId));
             td.Actions.Add(new ExecAction());
             using (var wiz = new TaskSchedulerWizard {
                 StartPosition = FormStartPosition.CenterParent
             })
             {
                 wiz.Initialize(task.TaskService, td);
                 wiz.AvailablePages       = TaskSchedulerWizard.AvailableWizardPages.IntroPage | TaskSchedulerWizard.AvailableWizardPages.SecurityPage | TaskSchedulerWizard.AvailableWizardPages.SummaryPage | TaskSchedulerWizard.AvailableWizardPages.TriggerEditPage | TaskSchedulerWizard.AvailableWizardPages.ActionEditPage;
                 wiz.RegisterTaskOnFinish = true;
                 wiz.TaskName             = $"Microsoft-Windows-TaskScheduler_Operational_Microsoft-Windows-TaskScheduler_{eventId}";
                 wiz.ShowDialog(this);
             }
         }
     }
 }
        internal static void WizardTest(TaskService ts, System.IO.TextWriter output, params string[] arg)
        {
            try
            {
                string FolderName = "My Folder";
                bool v2 = ts.HighestSupportedVersion > new Version(1, 1);
                var taskFolder = ts.RootFolder;
                if (v2)
                {
                    try
                    {
                        taskFolder = ts.GetFolder(FolderName);
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        taskFolder = ts.RootFolder.CreateFolder(FolderName);
                    }
                }

                using (var taskSchedulerWizard = new TaskSchedulerWizard())
                {
                    var newTaskDefinition = ts.NewTask();
                    newTaskDefinition.Actions.Add(new ExecAction("notepad.exe"));
                    taskSchedulerWizard.Initialize(ts, newTaskDefinition);
                    taskSchedulerWizard.TaskFolder = FolderName;
                    taskSchedulerWizard.RegisterTaskOnFinish = true;
                    taskSchedulerWizard.AvailableTriggers = TaskSchedulerWizard.AvailableWizardTriggers.Event;
                    taskSchedulerWizard.AvailablePages = TaskSchedulerWizard.AvailableWizardPages.IntroPage |
                        //TaskSchedulerWizard.AvailableWizardPages.TriggerSelectPage |
                        TaskSchedulerWizard.AvailableWizardPages.TriggerEditPage |
                        //TaskSchedulerWizard.AvailableWizardPages.TriggerPropertiesPage |
                        TaskSchedulerWizard.AvailableWizardPages.ActionEditPage |
                        //TaskSchedulerWizard.AvailableWizardPages.SecurityPage |
                        TaskSchedulerWizard.AvailableWizardPages.SummaryPage;

                    if (taskSchedulerWizard.ShowDialog() == DialogResult.OK)
                        taskFolder.DeleteTask(taskSchedulerWizard.Task.Name);
                    //    _tlv.Tasks = taskFolder.Tasks;
                }

                // Create a new task definition and assign properties
                /*TaskSchedulerWizard wiz = new TaskSchedulerWizard(ts, null, true) { TaskFolder = @"\Microsoft" };
                if (wiz.ShowDialog() == DialogResult.OK)
                {
                    Task t = wiz.Task;
                    if (t.Definition.Triggers.Count > 1)
                        new TaskEditDialog(t).ShowDialog();
                    else
                    {
                        wiz.AvailablePages = TaskSchedulerWizard.AvailableWizardPages.TriggerPropertiesPage | TaskSchedulerWizard.AvailableWizardPages.TriggerSelectPage | TaskSchedulerWizard.AvailableWizardPages.SummaryPage;
                        wiz.AvailableTriggers = TaskSchedulerWizard.AvailableWizardTriggers.Daily | TaskSchedulerWizard.AvailableWizardTriggers.Time | TaskSchedulerWizard.AvailableWizardTriggers.Weekly | TaskSchedulerWizard.AvailableWizardTriggers.Monthly | TaskSchedulerWizard.AvailableWizardTriggers.MonthlyDOW;
                        wiz.AllowEditorOnFinish = true;
                        wiz.EditorOnFinishText = "Show dialog";
                        wiz.TriggerPagePrompt = "When???";
                        wiz.RegisterTaskOnFinish = true;
                        wiz.SummaryRegistrationNotice = "Done when you click Finish";
                        wiz.SummaryFormatString = "Name: {0}\r\nDescription: {1}\r\nTrigger: {2}";
                        wiz.Title = "My Wizard";
                        wiz.Initialize(t);
                        wiz.ShowDialog();
                    }
                }

                if (wiz.Task != null)
                    ts.RootFolder.DeleteTask(wiz.Task.Path);*/
            }
            catch (Exception ex)
            {
                output.WriteLine(ex.ToString());
            }
        }