简体   繁体   English

列出在启动和登录时触发的计划任务C#

[英]List Scheduled Task triggered at startup and logon c#

I am using http://www.codeproject.com/KB/cs/tsnewlib.aspx library to list task which are triggered at log on or startup 我正在使用http://www.codeproject.com/KB/cs/tsnewlib.aspx库列出在登录或启动时触发的任务

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 我应使用什么条件仅列出启动或登录任务

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. 您需要枚举每个taskTriggers ,或者对该任务执行某些操作,将其存储以备后用,或者将该名称存储在已过滤的Task对象名称列表中。 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. 您的选择过程是检查TriggerOnLogonTrigger还是OnSystemStartTrigger然后可以在最终列表中选择该任务。

eg 例如

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. 当然,如果您想要仅具有登录或启动类型的任务,则还要检查一下Triggers集合中是否只有1个Triggers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM