Using Harmony Function Action Command.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
11 using Microsoft.Win32;
12 using SharpDisplayManager.TaskScheduler;
15 using System.Runtime.InteropServices;
16 using System.Security;
17 using System.Security.Principal;
18 using System.Windows.Forms;
19 using System.Deployment.Application;
21 namespace SharpDisplayManager
23 public class StartupManager
25 private TaskSchedulerClass scheduler;
27 private bool isAvailable;
29 private const string REGISTRY_RUN =
30 @"Software\Microsoft\Windows\CurrentVersion\Run";
32 private bool IsAdministrator()
36 WindowsIdentity identity = WindowsIdentity.GetCurrent();
37 WindowsPrincipal principal = new WindowsPrincipal(identity);
38 return principal.IsInRole(WindowsBuiltInRole.Administrator);
46 public StartupManager()
48 int p = (int)System.Environment.OSVersion.Platform;
49 if ((p == 4) || (p == 128))
56 if (IsAdministrator())
60 scheduler = new TaskSchedulerClass();
61 scheduler.Connect(null, null, null, null);
68 if (scheduler != null)
72 // check if the task scheduler is running
73 IRunningTaskCollection collection = scheduler.GetRunningTasks(0);
75 ITaskFolder folder = scheduler.GetFolder("\\Sharp Display Manager");
76 IRegisteredTask task = folder.GetTask("Startup");
77 startup = (task != null) &&
78 (task.Definition.Triggers.Count > 0) &&
79 (task.Definition.Triggers[1].Type ==
80 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
81 (task.Definition.Actions.Count > 0) &&
82 (task.Definition.Actions[1].Type ==
83 TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
84 (task.Definition.Actions[1] as IExecAction != null) &&
85 ((task.Definition.Actions[1] as IExecAction).Path ==
86 Application.ExecutablePath);
92 catch (UnauthorizedAccessException)
107 if (scheduler == null)
111 using (RegistryKey key =
112 Registry.CurrentUser.OpenSubKey(REGISTRY_RUN))
117 string value = (string)key.GetValue("SharpDisplayManager");
119 startup = value == LaunchCommand;
124 catch (SecurityException)
135 private void CreateSchedulerTask()
137 ITaskDefinition definition = scheduler.NewTask(0);
138 definition.RegistrationInfo.Description =
139 "This task starts the Sharp Display Manager on Windows startup.";
140 definition.Principal.RunLevel =
141 TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
142 definition.Settings.DisallowStartIfOnBatteries = false;
143 definition.Settings.StopIfGoingOnBatteries = false;
144 definition.Settings.ExecutionTimeLimit = "PT0S";
146 ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
147 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
149 IExecAction action = (IExecAction)definition.Actions.Create(
150 TASK_ACTION_TYPE.TASK_ACTION_EXEC);
151 action.Path = Application.ExecutablePath;
152 action.WorkingDirectory =
153 Path.GetDirectoryName(Application.ExecutablePath);
155 ITaskFolder root = scheduler.GetFolder("\\");
159 folder = root.GetFolder("Sharp Display Manager");
163 folder = root.CreateFolder("Sharp Display Manager", "");
165 folder.RegisterTaskDefinition("Startup", definition,
166 (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
167 TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
170 private void DeleteSchedulerTask()
172 ITaskFolder root = scheduler.GetFolder("\\");
175 ITaskFolder folder = root.GetFolder("Sharp Display Manager");
176 folder.DeleteTask("Startup", 0);
178 catch (IOException) { }
181 root.DeleteFolder("Sharp Display Manager", 0);
183 catch (IOException) { }
190 //Executable path won't launch ClickOnce Application with deployment enabled.
191 //return Application.ExecutablePath;
192 //Instead we need to launch the application using the .appref-ms shortcut.
193 //That shortcut is located at <programs>\<publisher>\<suite>\<product>.appref-ms
194 return string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\", "Slions", "\\", "Sharp Display Manager" , "\\" ,"Sharp Display Manager", ".appref-ms");
198 private void CreateRegistryRun()
200 RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
201 //Rather than the executable name we pass in the ClickOnce shortcut to make sure we launch with deployment support
202 key.SetValue("SharpDisplayManager", LaunchCommand);
205 private void DeleteRegistryRun()
207 RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
208 key.DeleteValue("SharpDisplayManager");
211 public bool IsAvailable
213 get { return isAvailable; }
224 if (startup != value)
228 if (scheduler != null)
231 CreateSchedulerTask();
233 DeleteSchedulerTask();
246 catch (UnauthorizedAccessException)
248 throw new InvalidOperationException();
254 throw new InvalidOperationException();