Skip to content

Commit

Permalink
fixup! remove task schedulled autostart to use only registry way
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiye committed Dec 27, 2024
1 parent 4a117ce commit 954328f
Showing 1 changed file with 40 additions and 84 deletions.
124 changes: 40 additions & 84 deletions OpenHardwareMonitor/UI/StartupManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Windows.Forms;
Expand All @@ -11,15 +10,14 @@ namespace OpenHardwareMonitor.UI
public class StartupManager
{

private readonly TaskSchedulerClass _scheduler;
private bool _startup;
private const string REGISTRY_RUN = @"Software\Microsoft\Windows\CurrentVersion\Run";

private bool IsAdministrator()
{
try
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
var identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
Expand All @@ -33,7 +31,6 @@ public StartupManager()
{
if (Software.OperatingSystem.IsUnix)
{
_scheduler = null;
IsAvailable = false;
return;
}
Expand All @@ -42,99 +39,58 @@ public StartupManager()
{
try
{
_scheduler = new TaskSchedulerClass();
_scheduler.Connect(null, null, null, null);
var _scheduler = new TaskSchedulerClass();
_scheduler.Connect();

var folder = _scheduler.GetFolder("\\Open Hardware Monitor");
var task = folder.GetTask("Startup");
_startup = task != null &&
task.Definition.Triggers.Count > 0 &&
task.Definition.Triggers[1].Type == TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON &&
task.Definition.Actions.Count > 0 &&
task.Definition.Actions[1].Type == TASK_ACTION_TYPE.TASK_ACTION_EXEC &&
task.Definition.Actions[1] is IExecAction execAction &&
execAction.Path == Application.ExecutablePath;

if (_startup)
{
//old versions compatibility - convert task to registry
DeleteSchedulerTask(_scheduler);
CreateRegistryRun();
}
}
catch
{
_scheduler = null;
}

if (_scheduler != null)
{
try
{
try
{
// check if the taskscheduler is running
IRunningTaskCollection collection = _scheduler.GetRunningTasks(0);
}
catch (ArgumentException) { }

ITaskFolder folder = _scheduler.GetFolder("\\Open Hardware Monitor");
IRegisteredTask task = folder.GetTask("Startup");
_startup = task != null &&
task.Definition.Triggers.Count > 0 &&
task.Definition.Triggers[1].Type == TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON &&
task.Definition.Actions.Count > 0 &&
task.Definition.Actions[1].Type == TASK_ACTION_TYPE.TASK_ACTION_EXEC &&
task.Definition.Actions[1] is IExecAction execAction &&
execAction.Path == Application.ExecutablePath;

if (_startup)
{
//old versions compatibility - convert task to registry
DeleteSchedulerTask();
CreateRegistryRun();
}
}
catch (IOException)
{
_startup = false;
}
catch (UnauthorizedAccessException)
{
_scheduler = null;
}
catch (COMException)
{
_scheduler = null;
}
catch (NotImplementedException)
{
_scheduler = null;
}
_startup = false;
}
}
else
{
_scheduler = null;
}

if (_scheduler == null)
try
{
try
using (var key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN))
{
using (RegistryKey key =
Registry.CurrentUser.OpenSubKey(REGISTRY_RUN))
_startup = false;
if (key != null)
{
_startup = false;
if (key != null)
{
string value = (string)key.GetValue("OpenHardwareMonitor");
if (value != null)
_startup = value == Application.ExecutablePath;
}
var value = (string)key.GetValue("OpenHardwareMonitor");
if (value != null)
_startup = value == Application.ExecutablePath;
}
IsAvailable = true;
}
catch (SecurityException)
{
IsAvailable = false;
}
IsAvailable = true;
}
else
catch (SecurityException)
{
IsAvailable = true;
IsAvailable = false;
}
}

private void DeleteSchedulerTask()
private static void DeleteSchedulerTask(TaskSchedulerClass scheduler)
{
ITaskFolder root = _scheduler.GetFolder("\\");
var root = scheduler.GetFolder("\\");
try
{
ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
var folder = root.GetFolder("Open Hardware Monitor");
folder.DeleteTask("Startup", 0);
}
catch (IOException) { }
Expand All @@ -145,16 +101,16 @@ private void DeleteSchedulerTask()
catch (IOException) { }
}

private void CreateRegistryRun()
private static void CreateRegistryRun()
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
var key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
key?.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
}

private void DeleteRegistryRun()
private static void DeleteRegistryRun()
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
key.DeleteValue("OpenHardwareMonitor");
var key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
key?.DeleteValue("OpenHardwareMonitor");
}

public bool IsAvailable { get; }
Expand Down

0 comments on commit 954328f

Please sign in to comment.