Question

I am using http://www.codeproject.com/KB/cs/tsnewlib.aspx library to list task which are triggered at log on or startup

ScheduledTasks st = new ScheduledTasks();

            foreach (string taskName in st.GetTaskNames())
            {
                using (Task task = st.OpenTask(taskName))
                {

                        listBox1.Items.Add(taskName);

                }
            }

What condition shall I use to list only startup or logon tasks

Was it helpful?

Solution

You need to enumerate the Triggers for each task and either do something with the task, store it for later, or store the name in a list of your filtered Task object names. Your selection process is to check if Trigger is either a OnLogonTrigger or a OnSystemStartTrigger then you can select that task into your final List.

e.g.

List<string> startupAndLogonTasks = new List<string>();
foreach (string taskName in st.GetTaskNames()) {
    using (Task task = st.OpenTask(taskName)) {
        if (task != null) {
            foreach (Trigger tr in task.Triggers) {
                if (tr is OnSystemStartTrigger || tr is OnLogonTrigger) {
                    //  Do something, such as log the name, or store the task for later
                    startupAndLogonTasks.Add(task.Name);
                    //  break out and move to the next task
                    break;
                }
            }
        }
    }
}

Of course, if you want tasks that only have the logon or startup type, then also check you only have 1 trigger in the Triggers collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top