2011-09-12 8 views
104

현재 C# WPF 프로젝트를 작성 중입니다. 사용자가 Windows 작업 스케줄러에 예약 된 작업을 만들고 추가 할 수있게해야합니다.예약 된 작업 생성

어떻게하면이 일을 할 수 있고 인터넷 검색시 많은 것을 찾지 않아서 지침과 참조를 사용해야 할 필요가 있습니다.

+1

필요한 항목은 다음과 같습니다. http://msdn.microsoft.com/en-us/library/aa383614(v=vs.85).aspx. 프로그래밍 방식으로 필요한 것을 달성하는 방법에 대한 API, 예제 및 설명. – kroonwijk

답변

171

당신은 Task Scheduler Managed Wrapper를 사용할 수 있습니다

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

는 다른 방법이 native API를 사용하거나 Quartz.NET 갈 수 있습니다. 자세한 내용은 this을 참조하십시오. 나 https://www.nuget.org/packages/ASquare.WindowsTaskScheduler/

그것은 잘 유창함 API를 설계하기위한

+2

예, Microsoft.Win32.TaskScheduler.dll을 다운로드하고 참조해야합니다. 링크가 대답 안에 있습니다. – Dmitry

+0

그래, 미안하지만 내가 참고를 추가했다고 생각했는데 어떤 이유로 그것이 아니었다. 죄송합니다. 도와 주셔서 감사합니다 – Boardy

+1

@ 드미트리 어떻게 작업을 시작합니까? Windows 스케줄러 등으로 등록해야합니까? – Haroon

17

이 작동합니다.

//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(); 
+1

이 정보를 SQL 서버 데이터베이스에 저장할 수 있습니까? – Fearcoder