除了星期日在Windows任务计划程序中,每天安排任务

时间:2014-01-13 12:01:01

标签: c# scheduled-tasks

为了在Windows任务计划程序中每周安排一次任务(每周一次),我使用codeplex中的以下代码:

            TaskDefinition td = ts.NewTask();
            DateTime t = ts.RootFolder.Tasks["Test"].LastRunTime;
            td.RegistrationInfo.Description = "Does something";

            td.Triggers.Add(new TimeTrigger(DateTime.Now + TimeSpan.FromSeconds(10)));
            //td.StartBoundary = DateTime.Today + TimeSpan.FromHours(23);

            td.Triggers.Add(new WeeklyTrigger
            {
                StartBoundary = DateTime.Today + TimeSpan.FromHours(2),
                DaysOfWeek = DaysOfTheWeek.Friday
            });


            // Create an action that will launch Notepad whenever the trigger fires
            td.Actions.Add(new ExecAction("D:\\Tasks\\obj\\x86\\Debug\\Tasks.exe", "SPU", null));

            // Register the task in the root folder
            ts.RootFolder.RegisterTaskDefinition(@"Test", td);

如何安排一周6天(周日除外)执行的任务?

1 个答案:

答案 0 :(得分:2)

我还没有尝试过,但DaysOfTheWeek标有Flags属性,因此您应该可以指定多天这样:

DaysOfWeek = DaysOfTheWeek.Monday | DaysOfTheWeek.Tuesday | DaysOfTheWeek.Wednesday
             | DaysOfTheWeek.Thursday | DaysOfTheWeek.Friday | DaysOfTheWeek.Saturday