Startup using scheduled tasks

This commit is contained in:
2dust
2022-10-13 20:29:26 +08:00
parent 3407b4dddd
commit b45d0b15ba
4 changed files with 67 additions and 4 deletions

View File

@@ -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; }

View File

@@ -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
}
}
/// <summary>
/// Auto Start via TaskService
/// </summary>
/// <param name="taskName"></param>
/// <param name="fileName"></param>
/// <param name="description"></param>
/// <exception cref="ArgumentNullException"></exception>
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

View File

@@ -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;

View File

@@ -20,7 +20,8 @@
<PackageReference Include="NHotkey" Version="2.1.0" />
<PackageReference Include="NHotkey.Wpf" Version="2.1.0" />
<PackageReference Include="QRCoder.Xaml" Version="1.4.3" />
<PackageReference Include="YamlDotNet" Version="12.0.1" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
<PackageReference Include="YamlDotNet" Version="12.0.2" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.10" />
<PackageReference Include="ReactiveUI.Fody" Version="18.3.1" />
<PackageReference Include="ReactiveUI.Validation" Version="3.0.1" />