■ TaskService 클래스를 사용해 매일 일정 시간 실행 작업을 등록하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System.Diagnostics; using Microsoft.Win32.TaskScheduler; namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string taskPath = "ICODEBROKER\\메모장실행"; using(TaskService taskService = new TaskService()) { Microsoft.Win32.TaskScheduler.Task task = taskService.GetTask(taskPath); if(task == null) { TaskDefinition taskDefinition = taskService.NewTask(); taskDefinition.Principal.DisplayName = "메모장 실행"; taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken; taskDefinition.Principal.UserId = $"{Environment.UserDomainName}\\{Environment.UserName}"; taskDefinition.Principal.RunLevel = TaskRunLevel.Highest; taskDefinition.RegistrationInfo.Description = "윈도우즈 로그인시 메모장을 실행합니다."; taskDefinition.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew; taskDefinition.Settings.DisallowStartIfOnBatteries = false; taskDefinition.Settings.StopIfGoingOnBatteries = false; taskDefinition.Settings.AllowHardTerminate = false; taskDefinition.Settings.StartWhenAvailable = false; taskDefinition.Settings.RunOnlyIfNetworkAvailable = false; taskDefinition.Settings.IdleSettings.StopOnIdleEnd = false; taskDefinition.Settings.IdleSettings.RestartOnIdle = false; taskDefinition.Settings.AllowDemandStart = false; taskDefinition.Settings.RunOnlyIfIdle = false; taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.Zero; taskDefinition.Settings.Priority = ProcessPriorityClass.High; taskDefinition.Settings.Hidden = false; taskDefinition.Settings.Enabled = true; DailyTrigger trigger = new DailyTrigger(); trigger.DaysInterval = 1; trigger.StartBoundary = new DateTime(2022, 7, 10, 5, 0, 0); trigger.EndBoundary = new DateTime(2022, 7, 31); taskDefinition.Triggers.Add(trigger); taskDefinition.Actions.Add(new ExecAction("notepad.exe")); taskService.RootFolder.RegisterTaskDefinition(taskPath, taskDefinition); } } } #endregion } |