从任务计划程序获取参数
Get arguments From Task Scheduler
我可以这样创建一个新任务:
Friend Sub CreateTaskF(ct_NameTask As String, ct_DescriptionTask As String, ct_Hour As Integer, ct_Minut As Integer, ct_date As String, ct_arg As String)
Dim ts As TaskService = New TaskService
Dim starttime
starttime = ct_date & "T" & ct_Hour & ":" & ct_Minut & ":00"
Dim td As TaskDefinition = ts.NewTask
td.RegistrationInfo.Description = ct_DescriptionTask
td.Triggers.Add(New DailyTrigger With {.StartBoundary = StartTime})
td.Actions.Add(New ExecAction(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString & "\Roaming\Extractor.exe", ct_arg, Nothing))
ts.RootFolder.RegisterTaskDefinition(ct_NameTask, td)
End Sub
但现在我需要获取任务计划程序中所有任务的参数。这时我收到了任务的数量和它们的名称,但我不知道如何获得参数。
我需要帮助
我脑袋磕了几天,终于有了解决办法。我仍然无法获得参数,所以我创建了服务,获得了所有任务,并循环遍历了一组任务,在循环中,我得到了一个任务文件,我读取了 XML 文件,然后从那里我了解我正在处理的任务的所有信息。
Friend Function listTask() As DataTable
Dim dtTasks As New DataTable
dtTasks.Columns.Add("NAME")
dtTasks.Columns.Add("STATE")
dtTasks.Columns.Add("ARGS")
Dim service
service = CreateObject("Schedule.Service")
Call service.Connect()
' Get the task folder that contains the tasks.
Dim rootFolder
rootFolder = service.GetFolder("\")
Dim taskCollection 'As Microsoft.Win32.TaskScheduler.TaskCollection
taskCollection = rootFolder.GetTasks(0)
Dim numberOfTasks As Integer
numberOfTasks = taskCollection.Count
If numberOfTasks = 0 Then
MsgBox("Não Existem Tarefas")
Else
Dim count As Integer = 0
Dim registeredTask
For Each registeredTask In taskCollection
'For i = 0 To numberOfTasks - 1
Dim taskState As String
Select Case registeredTask.State
Case "0"
taskState = "Unknown"
Case "1"
taskState = "Disabled"
Case "2"
taskState = "Queued"
Case "3"
taskState = "Ready"
Case "4"
taskState = "Running"
End Select
'TENHO AQUI TUDO !!!!!
Dim aaaa As String = registeredTask.xml
Dim BodyDoc As XmlDocument = New XmlDocument()
BodyDoc.LoadXml(aaaa)
Dim ReaderDoc As XmlNodeReader = New XmlNodeReader(BodyDoc)
'TENTAR METER NO DATASET
Dim ds As DataSet = New DataSet()
ds.ReadXml(ReaderDoc)
ReaderDoc.Close()
dtTasks.Rows.Add()
dtTasks.Rows(count).Item("NAME") = ds.Tables("RegistrationInfo").Rows(0).Item("URI").ToString
dtTasks.Rows(count).Item("STATE") = taskState.ToString
Dim column As DataColumnCollection = ds.Tables("Exec").Columns
If column.Contains("Arguments") Then
dtTasks.Rows(count).Item("ARGS") = ds.Tables("Exec").Rows(0).Item("Arguments").ToString
Else
dtTasks.Rows(count).Item("ARGS") = ""
End If
count = count + 1
Next
End If
Return dtTasks
End Function
我可以这样创建一个新任务:
Friend Sub CreateTaskF(ct_NameTask As String, ct_DescriptionTask As String, ct_Hour As Integer, ct_Minut As Integer, ct_date As String, ct_arg As String)
Dim ts As TaskService = New TaskService
Dim starttime
starttime = ct_date & "T" & ct_Hour & ":" & ct_Minut & ":00"
Dim td As TaskDefinition = ts.NewTask
td.RegistrationInfo.Description = ct_DescriptionTask
td.Triggers.Add(New DailyTrigger With {.StartBoundary = StartTime})
td.Actions.Add(New ExecAction(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString & "\Roaming\Extractor.exe", ct_arg, Nothing))
ts.RootFolder.RegisterTaskDefinition(ct_NameTask, td)
End Sub
但现在我需要获取任务计划程序中所有任务的参数。这时我收到了任务的数量和它们的名称,但我不知道如何获得参数。
我需要帮助
我脑袋磕了几天,终于有了解决办法。我仍然无法获得参数,所以我创建了服务,获得了所有任务,并循环遍历了一组任务,在循环中,我得到了一个任务文件,我读取了 XML 文件,然后从那里我了解我正在处理的任务的所有信息。
Friend Function listTask() As DataTable
Dim dtTasks As New DataTable
dtTasks.Columns.Add("NAME")
dtTasks.Columns.Add("STATE")
dtTasks.Columns.Add("ARGS")
Dim service
service = CreateObject("Schedule.Service")
Call service.Connect()
' Get the task folder that contains the tasks.
Dim rootFolder
rootFolder = service.GetFolder("\")
Dim taskCollection 'As Microsoft.Win32.TaskScheduler.TaskCollection
taskCollection = rootFolder.GetTasks(0)
Dim numberOfTasks As Integer
numberOfTasks = taskCollection.Count
If numberOfTasks = 0 Then
MsgBox("Não Existem Tarefas")
Else
Dim count As Integer = 0
Dim registeredTask
For Each registeredTask In taskCollection
'For i = 0 To numberOfTasks - 1
Dim taskState As String
Select Case registeredTask.State
Case "0"
taskState = "Unknown"
Case "1"
taskState = "Disabled"
Case "2"
taskState = "Queued"
Case "3"
taskState = "Ready"
Case "4"
taskState = "Running"
End Select
'TENHO AQUI TUDO !!!!!
Dim aaaa As String = registeredTask.xml
Dim BodyDoc As XmlDocument = New XmlDocument()
BodyDoc.LoadXml(aaaa)
Dim ReaderDoc As XmlNodeReader = New XmlNodeReader(BodyDoc)
'TENTAR METER NO DATASET
Dim ds As DataSet = New DataSet()
ds.ReadXml(ReaderDoc)
ReaderDoc.Close()
dtTasks.Rows.Add()
dtTasks.Rows(count).Item("NAME") = ds.Tables("RegistrationInfo").Rows(0).Item("URI").ToString
dtTasks.Rows(count).Item("STATE") = taskState.ToString
Dim column As DataColumnCollection = ds.Tables("Exec").Columns
If column.Contains("Arguments") Then
dtTasks.Rows(count).Item("ARGS") = ds.Tables("Exec").Rows(0).Item("Arguments").ToString
Else
dtTasks.Rows(count).Item("ARGS") = ""
End If
count = count + 1
Next
End If
Return dtTasks
End Function