Trying to find out why the code works on some machines and other it doesn't is turning into a real pain in the rear end.
Here's my code that I'm using. Like I said, it works on most machines with no issues but others... oops, something went wrong.
Here's my code that I'm using. Like I said, it works on most machines with no issues but others... oops, something went wrong.
Dim taskService As New TaskService()
Dim newTask As TaskDefinition = taskService.NewTask
Dim logonTriggerDefinition As LogonTrigger = New LogonTrigger
newTask.Triggers.Add(logonTriggerDefinition)
newTask.RegistrationInfo.Description = "My Task"
newTask.Principal.RunLevel = TaskRunLevel.Highest
newTask.Principal.LogonType = TaskLogonType.InteractiveToken
Dim exePathInfo As New IO.FileInfo(Application.ExecutablePath)
newTask.Actions.Add(New ExecAction(exePathInfo.FullName, "", exePathInfo.DirectoryName))
exePathInfo = Nothing
With newTask.Settings
.Compatibility = TaskCompatibility.V2
.AllowDemandStart = True
.DisallowStartIfOnBatteries = False
.RunOnlyIfIdle = False
.StopIfGoingOnBatteries = False
.AllowHardTerminate = False
.ExecutionTimeLimit = Nothing
End With
Try
newTask.Validate(True)
Catch ex As InvalidOperationException
newTask.Dispose()
taskService.Dispose()
Functions.eventLogFunctions.writeToSystemEventLog("There was an error while validating the task definition settings.", EventLogEntryType.Error)
Functions.eventLogFunctions.writeCrashToEventLog(ex)
MsgBox("There was an error while validating the task definition settings. Please see the Application Event Log for more details.", MsgBoxStyle.Critical, Me.Text)
Exit Sub
End Try
Dim taskFolderObject As TaskFolder = If(taskService.GetFolder("myTaskFolder"), taskService.RootFolder.CreateFolder("myTaskFolder"))
taskFolderObject.RegisterTaskDefinition("My Task", newTask)
newTask.Dispose()
taskService.Dispose()