This project has moved. For the latest updates, please go here.

Change Task Name

Topics: Errors
Sep 25, 2015 at 7:28 PM
Edited Sep 25, 2015 at 7:28 PM
I'm trying to figure out the best way to add new tasks that are painless for the user but I'm running into an issue with my code where the Task Name can't be changed

Here is my code:
Public Sub newTask()

        Dim td As TaskDefinition = _tskSrvc.NewTask
        td.RegistrationInfo.Description = ""

        td.Actions.Add(New ExecAction("notepad.exe", "c:\test.log"))

        ' Register the task in the root folder
        _rootFolder.RegisterTaskDefinition("New Task Service Monitor Task", td)

        Dim dlg As New TaskEditDialog(_rootFolder.Tasks("New Task Service Monitor Task"), True, True)
        If dlg.ShowDialog() = vbCancel Then

            _rootFolder.DeleteTask("New Task Service Monitor Task")

        End If

    End Sub
But when the dialog box pops up I would like the user to be able to change the Task name but even with the task as editable it does not allow for this. Is there anyway to over this? Thanks
Coordinator
Sep 27, 2015 at 6:09 AM
There is not a simple way to do it. The only way I know how is to get the TaskDefinition from one task, delete the one with the original name, and then re-register a task with the new name and the TaskDefinition pulled off the old task.
Sep 28, 2015 at 2:52 PM
So I worked on the code a bit and so far the best way I came up with was this:
    Public Sub newTask()

        ' Create a new task definition and assign properties
        Dim td As TaskDefinition = _tskSrvc.NewTask
        td.RegistrationInfo.Description = ""

        ' Add an action (shorthand) that runs Notepad
        td.Actions.Add(New ExecAction("notepad.exe", "c:\test.log"))

        ' Register the task in the root folder
        _rootFolder.RegisterTaskDefinition("THISISNTANEWTASK", td)

        Dim dlg As New TaskEditDialog(_rootFolder.Tasks("THISISNTANEWTASK"), True, True)
        _rootFolder.DeleteTask("THISISNTANEWTASK")
        dlg.TaskName = InputBox("Please enter name of Task")

        If dlg.ShowDialog() = vbOK Then

            _rootFolder.RegisterTaskDefinition(dlg.TaskName, dlg.TaskDefinition)

        End If

    End Sub
Hope this helps somebody else.
Marked as answer by dahall on 11/9/2015 at 11:05 AM