예약 된 작업 만들기 중입니다. 사용자가

C # WPF 프로젝트를 진행 중입니다. 사용자가 예약 된 작업을 만들어 Windows 작업 스케줄러에 추가 할 수 있도록해야합니다.

인터넷을 검색 할 때 많이 찾지 못해 어떻게 해야하는지, 지시문과 참조를 사용하는 방법은 무엇입니까?



답변

작업 스케줄러 관리 래퍼를 사용할 수 있습니다 .

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 the task at this time every other day
         td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

         // 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");
      }
   }
}

또는 네이티브 API 를 사용 하거나 Quartz.NET을 사용할 수 있습니다 . 자세한 내용은 이것을 참조 하십시오.


답변

이것은 나를 위해 작동합니다
https://www.nuget.org/packages/ASquare.WindowsTaskScheduler/

Fluent API를 훌륭하게 디자인했습니다.

//This will create Daily trigger to run every 10 minutes for a duration of 18 hours
SchedulerResponse response = WindowTaskScheduler
    .Configure()
    .CreateTask("TaskName", "C:\\Test.bat")
    .RunDaily()
    .RunEveryXMinutes(10)
    .RunDurationFor(new TimeSpan(18, 0, 0))
    .SetStartDate(new DateTime(2015, 8, 8))
    .SetStartTime(new TimeSpan(8, 0, 0))
    .Execute();


답변