Create win task to run once and delete immediately using C#
        Posted  
        
            by 
                pencilslate
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by pencilslate
        
        
        
        Published on 2012-11-06T22:43:14Z
        Indexed on 
            2012/11/06
            23:00 UTC
        
        
        Read the original article
        Hit count: 553
        
Here is the use case: - Create a new win task, run immediately and once complete, delete the task.
Here is basic code to create a task using C#.
    using (TaskService ts = new TaskService(null))
    {
        string projectName = "runnowtest" + Guid.NewGuid().ToString();
        //create new task
        TaskDefinition td = ts.NewTask();
        Trigger mt = null;
        //setup task as Registration trigger
        mt = td.Triggers.AddNew(TaskTriggerType.Registration);
        mt.StartBoundary = DateTime.Now;
        //delete the task 1 minute after the program ends
        td.Settings.DeleteExpiredTaskAfter = new TimeSpan(0, 1, 0); 
        //run the notepad++ in the task
        td.Actions.Add(new ExecAction("notepad.exe"));
        //register task
        Task output = ts.RootFolder.RegisterTaskDefinition(projectName, td);
        //check output
        Console.WriteLine(output != null ? "Task created" : "Task not created");
    }
The API doesn't seem to have a property/flag to mark task as run once. I am trying to ensure the above task runs only once and deletes immediately after that. Any thoughts are much appreciated!
© Stack Overflow or respective owner