Find out if scheduled task exists using Visual Basic .NET and System.Diagnostics.Process
rivate Function EncoderTaskExists(ByVal strTaskName As String) As Boolean
Try
Dim sCreateTask As New System.Diagnostics.Process
With sCreateTask
.StartInfo.FileName = “c:\windows\system32\schtasks.exe”
.StartInfo.Arguments = “/query /fo table”
.StartInfo.RedirectStandardOutput = True
.StartInfo.RedirectStandardError = True
.StartInfo.UseShellExecute = False
.Start()
.WaitForExit()
End With
If sCreateTask.ExitCode <> 0 Then
EventLog.WriteEntry(sCreateTask.StandardError.ReadToEnd)
Else
Dim sLine As String = sCreateTask.StandardOutput.ReadLine()
Do Until sLine Is Nothing
EncoderTaskExists = False
If InStr(sLine, strTaskName) > 0 Then
Return True
Exit Do
End If
sLine = sCreateTask.StandardOutput.ReadLine()
Loop
sLine = Nothing
End If
sCreateTask.Dispose()
sCreateTask = Nothing
Catch ex As Exception
EventLog.WriteEntry(ex.Message.ToString)
End Try
End Function