From 0c5464f8331e0f10fdd9b384c125b35075d60c9e Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Thu, 29 Oct 2020 14:47:45 +0800 Subject: [PATCH] refactor: extract RegisterNetchStartupItem() method --- Netch/Forms/SettingForm.cs | 48 +----------------------------------- Netch/Utils/Utils.cs | 50 +++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index aaf4e3d7..4386fa71 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -8,7 +8,6 @@ using System.Linq; using System.Net; using System.Threading.Tasks; using System.Windows.Forms; -using TaskScheduler; namespace Netch.Forms { @@ -357,52 +356,7 @@ namespace Netch.Forms #endregion - #region Register Startup Item - - var scheduler = new TaskSchedulerClass(); - scheduler.Connect(); - var folder = scheduler.GetFolder("\\"); - - var taskIsExists = false; - try - { - folder.GetTask("Netch Startup"); - taskIsExists = true; - } - catch (Exception) - { - // ignored - } - - if (Global.Settings.RunAtStartup) - { - if (taskIsExists) - folder.DeleteTask("Netch Startup", 0); - - var task = scheduler.NewTask(0); - task.RegistrationInfo.Author = "Netch"; - task.RegistrationInfo.Description = "Netch run at startup."; - task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; - - task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); - var action = (IExecAction) task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); - action.Path = Application.ExecutablePath; - - - task.Settings.ExecutionTimeLimit = "PT0S"; - task.Settings.DisallowStartIfOnBatteries = false; - task.Settings.RunOnlyIfIdle = false; - - folder.RegisterTaskDefinition("Netch Startup", task, (int) _TASK_CREATION.TASK_CREATE, null, null, - _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); - } - else - { - if (taskIsExists) - folder.DeleteTask("Netch Startup", 0); - } - - #endregion + Utils.Utils.RegisterNetchStartupItem(); Configuration.Save(); MessageBoxX.Show(i18N.Translate("Saved")); diff --git a/Netch/Utils/Utils.cs b/Netch/Utils/Utils.cs index 0e4ec4da..1ea8c64c 100644 --- a/Netch/Utils/Utils.cs +++ b/Netch/Utils/Utils.cs @@ -7,10 +7,12 @@ using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; +using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using MaxMind.GeoIP2; +using TaskScheduler; namespace Netch.Utils { @@ -96,7 +98,7 @@ namespace Netch.Utils { try { - var sha256 = System.Security.Cryptography.SHA256.Create(); + var sha256 = SHA256.Create(); var fileStream = File.OpenRead(filePath); return sha256.ComputeHash(fileStream).Aggregate(string.Empty, (current, b) => current + b.ToString("x2")); } @@ -237,5 +239,51 @@ namespace Netch.Utils break; } } + + public static void RegisterNetchStartupItem() + { + var scheduler = new TaskSchedulerClass(); + scheduler.Connect(); + var folder = scheduler.GetFolder("\\"); + + var taskIsExists = false; + try + { + folder.GetTask("Netch Startup"); + taskIsExists = true; + } + catch + { + // ignored + } + + if (Global.Settings.RunAtStartup) + { + if (taskIsExists) + folder.DeleteTask("Netch Startup", 0); + + var task = scheduler.NewTask(0); + task.RegistrationInfo.Author = "Netch"; + task.RegistrationInfo.Description = "Netch run at startup."; + task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; + + task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); + var action = (IExecAction) task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); + action.Path = Application.ExecutablePath; + + + task.Settings.ExecutionTimeLimit = "PT0S"; + task.Settings.DisallowStartIfOnBatteries = false; + task.Settings.RunOnlyIfIdle = false; + + folder.RegisterTaskDefinition("Netch Startup", task, (int) _TASK_CREATION.TASK_CREATE, null, null, + _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); + } + else + { + if (taskIsExists) + folder.DeleteTask("Netch Startup", 0); + } + } } } \ No newline at end of file