moel@82: /* moel@82: moel@82: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@82: moel@82: The contents of this file are subject to the Mozilla Public License Version moel@82: 1.1 (the "License"); you may not use this file except in compliance with moel@82: the License. You may obtain a copy of the License at moel@82: moel@82: http://www.mozilla.org/MPL/ moel@82: moel@82: Software distributed under the License is distributed on an "AS IS" basis, moel@82: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@82: for the specific language governing rights and limitations under the License. moel@82: moel@82: The Original Code is the Open Hardware Monitor code. moel@82: moel@82: The Initial Developer of the Original Code is moel@82: Michael Möller . moel@82: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@82: the Initial Developer. All Rights Reserved. moel@82: moel@82: Contributor(s): moel@82: moel@82: Alternatively, the contents of this file may be used under the terms of moel@82: either the GNU General Public License Version 2 or later (the "GPL"), or moel@82: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@82: in which case the provisions of the GPL or the LGPL are applicable instead moel@82: of those above. If you wish to allow use of your version of this file only moel@82: under the terms of either the GPL or the LGPL, and not to allow others to moel@82: use your version of this file under the terms of the MPL, indicate your moel@82: decision by deleting the provisions above and replace them with the notice moel@82: and other provisions required by the GPL or the LGPL. If you do not delete moel@82: the provisions above, a recipient may use your version of this file under moel@82: the terms of any one of the MPL, the GPL or the LGPL. moel@82: moel@82: */ moel@82: moel@82: using System; moel@82: using System.Collections.Generic; moel@82: using System.IO; moel@82: using System.Windows.Forms; moel@82: using Microsoft.Win32; moel@82: using OpenHardwareMonitor.TaskScheduler; moel@82: moel@82: namespace OpenHardwareMonitor.GUI { moel@82: public class StartupManager { moel@82: moel@82: private TaskSchedulerClass scheduler; moel@82: private bool startup; moel@82: moel@82: private const string REGISTRY_RUN = moel@82: @"Software\Microsoft\Windows\CurrentVersion\Run"; moel@82: moel@82: public StartupManager() { moel@82: try { moel@82: scheduler = new TaskSchedulerClass(); moel@82: scheduler.Connect(null, null, null, null); moel@104: } catch { moel@82: scheduler = null; moel@82: } moel@82: moel@82: if (scheduler != null) { moel@82: try { moel@82: ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor"); moel@82: IRegisteredTask task = folder.GetTask("Startup"); moel@82: startup = task != null; moel@101: } catch (IOException) { moel@82: startup = false; moel@82: } moel@82: } else { moel@82: RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN); moel@82: startup = false; moel@82: if (key != null) { moel@82: string value = (string)key.GetValue("OpenHardwareMonitor"); moel@82: if (value != null) moel@82: startup = value == Application.ExecutablePath; moel@82: } moel@82: } moel@82: } moel@82: moel@82: private void CreateSchedulerTask() { moel@82: ITaskDefinition definition = scheduler.NewTask(0); moel@82: definition.RegistrationInfo.Description = moel@82: "This task starts the Open Hardware Monitor on Windows startup."; moel@82: definition.Principal.RunLevel = moel@82: TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; moel@82: definition.Settings.DisallowStartIfOnBatteries = false; moel@82: definition.Settings.StopIfGoingOnBatteries = false; moel@82: definition.Settings.ExecutionTimeLimit = "PT0S"; moel@82: moel@82: ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create( moel@82: TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); moel@82: moel@82: IExecAction action = (IExecAction)definition.Actions.Create( moel@82: TASK_ACTION_TYPE.TASK_ACTION_EXEC); moel@82: action.Path = Application.ExecutablePath; moel@82: action.WorkingDirectory = moel@82: Path.GetDirectoryName(Application.ExecutablePath); moel@82: moel@82: ITaskFolder root = scheduler.GetFolder("\\"); moel@82: ITaskFolder folder; moel@82: try { moel@82: folder = root.GetFolder("Open Hardware Monitor"); moel@104: } catch (IOException) { moel@82: folder = root.CreateFolder("Open Hardware Monitor", ""); moel@82: } moel@82: folder.RegisterTaskDefinition("Startup", definition, moel@82: (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, moel@82: TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); moel@82: } moel@82: moel@82: private void DeleteSchedulerTask() { moel@82: ITaskFolder root = scheduler.GetFolder("\\"); moel@82: try { moel@82: ITaskFolder folder = root.GetFolder("Open Hardware Monitor"); moel@82: folder.DeleteTask("Startup", 0); moel@104: } catch (IOException) { } moel@82: try { moel@82: root.DeleteFolder("Open Hardware Monitor", 0); moel@104: } catch (IOException) { } moel@82: } moel@82: moel@82: private void CreateRegistryRun() { moel@82: RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN); moel@82: key.SetValue("OpenHardwareMonitor", Application.ExecutablePath); moel@82: } moel@82: moel@82: private void DeleteRegistryRun() { moel@82: RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN); moel@82: key.DeleteValue("OpenHardwareMonitor"); moel@82: } moel@82: moel@82: public bool Startup { moel@82: get { moel@82: return startup; moel@82: } moel@82: set { moel@82: if (startup != value) { moel@82: startup = value; moel@82: if (scheduler != null) { moel@82: if (startup) moel@82: CreateSchedulerTask(); moel@82: else moel@82: DeleteSchedulerTask(); moel@82: } else { moel@82: if (startup) moel@82: CreateRegistryRun(); moel@82: else moel@82: DeleteRegistryRun(); moel@82: } moel@82: } moel@82: } moel@82: } moel@82: } moel@82: moel@82: moel@82: moel@82: moel@82: moel@82: }