TaskSchedulerHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Microsoft.Win32.TaskScheduler;
  2. using System;
  3. using System.IO;
  4. using Task = Microsoft.Win32.TaskScheduler.Task;
  5. namespace Vinno.FIS.Sonopost.Upgrade
  6. {
  7. internal class TaskSchedulerHelper
  8. {
  9. private static readonly string _keeperFilePath = Path.Combine(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 1)).FullName, "Vinno.FIS.Sonopost.Keeper.exe");
  10. private static readonly string _taskName = "SonopostProtectionTask";
  11. /// <summary>
  12. /// 安装计划任务
  13. /// </summary>
  14. public static void Install()
  15. {
  16. if (IsTaskExist(_taskName))
  17. {
  18. UnInstall();
  19. }
  20. InstallTask();
  21. }
  22. /// <summary>
  23. /// 卸载计划任务
  24. /// </summary>
  25. public static void UnInstall()
  26. {
  27. if (IsTaskExist(_taskName))
  28. {
  29. UninstallTask();
  30. }
  31. }
  32. private static bool IsTaskExist(string serviceName)
  33. {
  34. using (TaskService ts = new TaskService())
  35. {
  36. // 查询指定任务
  37. Task task = ts.GetTask(_taskName);
  38. if (task != null)
  39. {
  40. return true;
  41. }
  42. else
  43. {
  44. return false;
  45. }
  46. }
  47. }
  48. private static void InstallTask()
  49. {
  50. using (TaskService ts = new TaskService())
  51. {
  52. // 创建新任务
  53. TaskDefinition td = ts.NewTask();
  54. td.RegistrationInfo.Description = "Sonopost Protection Task";
  55. // 设置触发器(每次系统启动时)会使钩子失效
  56. //BootTrigger bootTrigger = new BootTrigger();
  57. //td.Triggers.Add(bootTrigger);
  58. // 设置触发器: 任务注册时立即执行
  59. RegistrationTrigger registrationTrigger = new RegistrationTrigger();
  60. registrationTrigger.StartBoundary = DateTime.Now;
  61. td.Triggers.Add(registrationTrigger);
  62. LogonTrigger logonTrigger = new LogonTrigger();
  63. td.Triggers.Add(logonTrigger);
  64. //TimeTrigger hourlyTrigger = new TimeTrigger { Repetition = new RepetitionPattern(TimeSpan.FromHours(1), TimeSpan.Zero) };
  65. //td.Triggers.Add(hourlyTrigger);
  66. td.Principal.RunLevel = TaskRunLevel.Highest;
  67. td.Principal.LogonType = TaskLogonType.InteractiveToken;
  68. td.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
  69. td.Settings.DisallowStartIfOnBatteries = false;
  70. td.Settings.StopIfGoingOnBatteries = false;
  71. td.Settings.ExecutionTimeLimit = TimeSpan.Zero;
  72. td.Settings.Compatibility = TaskCompatibility.V2_3;//Windows10配置
  73. // 设置要执行的操作
  74. td.Actions.Add(new ExecAction(_keeperFilePath, null, null));
  75. // 注册任务
  76. ts.RootFolder.RegisterTaskDefinition(_taskName, td, TaskCreation.CreateOrUpdate, @"Sonopost\VinnoUser", "VinnoTech", TaskLogonType.InteractiveToken);
  77. }
  78. }
  79. private static void UninstallTask()
  80. {
  81. using (TaskService ts = new TaskService())
  82. {
  83. // 检查任务是否存在并删除
  84. if (ts.GetTask(_taskName) != null)
  85. {
  86. ts.RootFolder.DeleteTask(_taskName);
  87. }
  88. }
  89. }
  90. }
  91. }