系统在C#中启动时触发任务调度程序

man*_*ngh 1 c# scheduled-tasks

我创建了一个任务调度程序并将其触发时间设置为固定,例如每天下午5:00,但是我想在系统启动或启动时触发该事件.如果您有任何示例,请帮助我使用代码.

提前致谢.

代码:------------------------------------------------ ------

 public static void CreateTask()
        {
            using (TaskService task = new TaskService())
            {`enter code here`
                TaskDefinition taskdDef = task.NewTask();

                taskdDef.RegistrationInfo.Description = "Does something";
                taskdDef.RegistrationInfo.Documentation = "http://www.mysite.com";

                taskdDef.Settings.ExecutionTimeLimit = new TimeSpan(0, 10, 0);
                taskdDef.Settings.AllowDemandStart = true;

                taskdDef.Actions.Add(new ExecAction(@"D:\Myfolder\bin\SGSclient.exe", "yourArguments", null));
                task.RootFolder.RegisterTaskDefinition("YourTask", taskdDef);
            }
        }
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 9

使用CodePlex中的任务计划程序管理器库,您可以编写此文件

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      // Get the service on the local machine
      using (TaskService ts = new TaskService())
      {
         // Create a new task definition and assign properties
         TaskDefinition td = ts.NewTask();
         td.RegistrationInfo.Description = "Does something";

         // Create a trigger that will fire after the system boot
         td.Triggers.Add(new BootTrigger() );

         // Create an action that will launch Notepad whenever the trigger fires
         td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

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

         // Remove the task we just created
         ts.RootFolder.DeleteTask("Test");
      }
   }
}
Run Code Online (Sandbox Code Playgroud)