C#如何获取触发器信息

C# How to get triggers information

如何从TaskService对象获取定时任务的触发器信息?

StringBuilder sb = new StringBuilder();
TaskService st = new TaskService();
var tasks = st.RootFolder.GetTasks();
foreach (Microsoft.Win32.TaskScheduler.Task task in tasks)
{ 
     sb.Append(task.Name+",");
     sb.Append(task.someValueHere <<<--- 
     sb.Append(task.LastRunTime + ",");
     sb.Append(task.NextRunTime.ToString()+" ; ");
}          
st.Dispose();

您需要访问 Definition,然后是 Triggers。例如:

foreach (var task in tasks)
{
    //...
    foreach (var trigger in task.Definition.Triggers)
    {
        //...access trigger properties here.
    }
}