コード例 #1
0
        /// <summary>
        /// Validates the Auto Dark Mode autostart entries for correctness
        /// </summary>
        /// <returns>true if the state is valid, false if the state was invalid</returns>
        public static ApiResponse Validate(bool alwaysValidate)
        {
            if (!builder.Config.Autostart.Validate && !alwaysValidate)
            {
                return(new()
                {
                    StatusCode = StatusCode.Ok,
                    Message = "validation disabled"
                });
            }
            //check if a logon task if used, needs to be reset if the folder is empty or the path differs from the execution base directory
            if (builder.Config.Tunable.UseLogonTask)
            {
                try
                {
                    string autostartPath = RegistryHandler.GetAutostartPath();
                    using Microsoft.Win32.TaskScheduler.Task logonTask = TaskSchdHandler.GetLogonTask();
                    if (logonTask == null)
                    {
                        Logger.Warn("autostart validation failed, missing logon task folder. fixing autostart");
                        ApiResponse result = AddAutostart(modified: true);
                        result.Details += "\nvalidation mode: recreate task (missing folder)";
                        return(result);
                    }
                    else if (logonTask.Definition.Actions.First().ToString().Trim() != Extensions.ExecutionPath)
                    {
                        Logger.Warn("autostart validation failed, wrong execution path. fixing autostart");
                        ApiResponse result = AddAutostart(modified: true);
                        result.Details += "\nvalidation mode: recreate task (wrong path)";
                        return(result);
                    }
                    else if (autostartPath != null)
                    {
                        Logger.Warn("autostart validation failed, wrong execution path. fixing autostart");
                        ApiResponse result = AddAutostart(modified: true);
                        result.Details += "\nvalidation mode: recreate task (regkey still active)";
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    string msg = "error during autostart validation:";
                    Logger.Error(ex, msg);
                    return(new()
                    {
                        StatusCode = StatusCode.Err,
                        Message = msg,
                        Details = $"Exception:\nMessage: {ex.Message}\nSource:{ex.Source}"
                    });
                }
            }
            //check if registry keys are used, needs to be reset if the key is missing or the path differs from the execution base directory
            else
            {
                bool   recreate = false;
                string reason   = "";
                try
                {
                    using Microsoft.Win32.TaskScheduler.Task logonTask = TaskSchdHandler.GetLogonTask();
                    if (logonTask != null)
                    {
                        recreate = true;
                        reason   = "logon task active in registry mode";
                    }
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, "could not determine task scheduler autostart state:");
                }
                if (!recreate)
                {
                    string autostartPath = RegistryHandler.GetAutostartPath();
                    if (autostartPath == null)
                    {
                        reason   = "missing entry";
                        recreate = true;
                    }
                    else
                    {
                        autostartPath = autostartPath.Replace("\"", "");
                        if (!autostartPath.Contains(Extensions.ExecutionPath))
                        {
                            reason   = "wrong path";
                            recreate = true;
                        }
                    }
                }

                if (recreate)
                {
                    Logger.Warn($"autostart validation failed ({reason}), recreate autostart registry keys");
                    ApiResponse result = AddAutostart(modified: true);
                    result.Details += $"\nvalidation mode: recreate regkey ({reason})";
                    return(result);
                }
            }
            return(new()
            {
                StatusCode = StatusCode.Ok,
                Message = "autostart entries valid"
            });
        }
コード例 #2
0
 public static ApiResponse GetAutostartState()
 {
     if (builder.Config.Tunable.UseLogonTask)
     {
         try
         {
             using Microsoft.Win32.TaskScheduler.Task logonTask = TaskSchdHandler.GetLogonTask();
             if (logonTask != null)
             {
                 ApiResponse response = new()
                 {
                     StatusCode = StatusCode.AutostartTask,
                     Message    = $"Enabled: {logonTask.Enabled}",
                     Details    = logonTask.Definition.Actions.ToString()
                 };
                 return(response);
             }
         }
         catch (Exception ex)
         {
             string msg = "error while getting logon task state:";
             Logger.Error(ex, msg);
             return(new()
             {
                 StatusCode = StatusCode.Err,
                 Message = msg,
                 Details = $"Exception:\nMessage: {ex.Message}\nSource:{ex.Source}"
             });
         }
     }
     else
     {
         string autostartPath = RegistryHandler.GetAutostartPath();
         if (autostartPath != null)
         {
             bool approved = RegistryHandler.IsAutostartApproved();
             if (approved)
             {
                 return(new()
                 {
                     StatusCode = StatusCode.AutostartRegistryEntry,
                     Message = $"Enabled",
                     Details = autostartPath.Replace("\"", "")
                 });
             }
             else
             {
                 return(new()
                 {
                     StatusCode = StatusCode.AutostartRegistryEntry,
                     Message = $"Disabled",
                     Details = autostartPath.Replace("\"", "")
                 });
             }
         }
     }
     return(new ApiResponse()
     {
         StatusCode = StatusCode.Disabled,
         Message = "no auto start entries found"
     });
 }