diff --git a/clashN/clashN/Mode/Config.cs b/clashN/clashN/Mode/Config.cs
index 969b9c4..e8a6a3a 100644
--- a/clashN/clashN/Mode/Config.cs
+++ b/clashN/clashN/Mode/Config.cs
@@ -31,6 +31,8 @@ namespace clashN.Mode
public bool allowLANConn { get; set; }
+ public bool autoRun { get; set; }
+
public bool enableStatistics { get; set; }
public string systemProxyExceptions { get; set; }
diff --git a/clashN/clashN/Tool/Utils.cs b/clashN/clashN/Tool/Utils.cs
index 6c665e0..7942fb4 100644
--- a/clashN/clashN/Tool/Utils.cs
+++ b/clashN/clashN/Tool/Utils.cs
@@ -1,6 +1,7 @@
using clashN.Base;
using clashN.Mode;
using Microsoft.Win32;
+using Microsoft.Win32.TaskScheduler;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
@@ -534,8 +535,25 @@ namespace clashN
{
try
{
- string exePath = GetExePath();
- RegWriteValue(autoRunRegPath, autoRunName, run ? $"\"{exePath}\"" : null);
+ //delete first
+ RegWriteValue(autoRunRegPath, autoRunName, null);
+ if (IsAdministrator())
+ {
+ AutoStart(autoRunName, "", "");
+ }
+
+ if (run)
+ {
+ string exePath = $"\"{GetExePath()}\"";
+ if (IsAdministrator())
+ {
+ AutoStart(autoRunName, exePath, "");
+ }
+ else
+ {
+ RegWriteValue(autoRunRegPath, autoRunName, exePath);
+ }
+ }
}
catch (Exception ex)
{
@@ -664,6 +682,47 @@ namespace clashN
}
}
+ ///
+ /// Auto Start via TaskService
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static void AutoStart(string taskName, string fileName, string description)
+ {
+ if (string.IsNullOrEmpty(taskName))
+ {
+ return;
+ }
+ string TaskName = taskName;
+ var logonUser = WindowsIdentity.GetCurrent().Name;
+ string taskDescription = description;
+ string deamonFileName = fileName;
+
+ using (var taskService = new TaskService())
+ {
+ var tasks = taskService.RootFolder.GetTasks(new Regex(TaskName));
+ foreach (var t in tasks)
+ {
+ taskService.RootFolder.DeleteTask(t.Name);
+ }
+ if (string.IsNullOrEmpty(fileName))
+ {
+ return;
+ }
+
+ var task = taskService.NewTask();
+ task.RegistrationInfo.Description = taskDescription;
+ task.Settings.DisallowStartIfOnBatteries = false;
+ task.Triggers.Add(new LogonTrigger { UserId = logonUser });
+ task.Principal.RunLevel = TaskRunLevel.Highest;
+ task.Actions.Add(new ExecAction(deamonFileName));
+
+ taskService.RootFolder.RegisterTaskDefinition(TaskName, task);
+ }
+ }
+
#endregion
#region 测速
diff --git a/clashN/clashN/ViewModels/SettingsViewModel.cs b/clashN/clashN/ViewModels/SettingsViewModel.cs
index f8403fc..efd7993 100644
--- a/clashN/clashN/ViewModels/SettingsViewModel.cs
+++ b/clashN/clashN/ViewModels/SettingsViewModel.cs
@@ -101,7 +101,7 @@ namespace clashN.ViewModels
}, this.IsValid());
//clashN
- AutoRun = Utils.IsAutoRun();
+ AutoRun = _config.autoRun;
EnableStatistics = _config.enableStatistics;
EnableSecurityProtocolTls13 = _config.enableSecurityProtocolTls13;
autoUpdateSubInterval = _config.autoUpdateSubInterval;
@@ -196,6 +196,7 @@ namespace clashN.ViewModels
//clashN
Utils.SetAutoRun(AutoRun);
+ _config.autoRun = AutoRun;
_config.enableStatistics = EnableStatistics;
_config.enableSecurityProtocolTls13 = EnableSecurityProtocolTls13;
_config.autoUpdateSubInterval = autoUpdateSubInterval;
diff --git a/clashN/clashN/clashN.csproj b/clashN/clashN/clashN.csproj
index a675632..f8b126f 100644
--- a/clashN/clashN/clashN.csproj
+++ b/clashN/clashN/clashN.csproj
@@ -20,7 +20,8 @@
-
+
+