Ejemplo n.º 1
1
        public void AddTask(int min, int hour, bool restart)
        {
            var taskService = new TaskService();

            //Create task definition
            var taskDefinition = taskService.NewTask();
            taskDefinition.RegistrationInfo.Description = min + "@" + hour + "@" + ((restart) ? "r" : "s");
            taskDefinition.Principal.UserId = "SYSTEM";

            var trigger = new DailyTrigger
            {
                StartBoundary = DateTime.Today + TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(min)
            };

            taskDefinition.Triggers.Add(trigger);

            //Create task action
            var fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"Power.exe");

            taskDefinition.Actions.Add(restart
                ? new ExecAction(fileName, "reboot \"This computer is going to reboot.\"")
                : new ExecAction(fileName, "shutdown \"This computer is going to shutdown to save power.\""));

            taskDefinition.Settings.AllowDemandStart = false;
            taskDefinition.Settings.DisallowStartIfOnBatteries = false;
            taskDefinition.Settings.DisallowStartOnRemoteAppSession = false;
            taskDefinition.Settings.StopIfGoingOnBatteries = false;

            taskService.RootFolder.RegisterTaskDefinition(@"FOG\" + taskDefinition.RegistrationInfo.Description,
                taskDefinition);
            taskService.Dispose();
        }
Ejemplo n.º 2
0
 public void RemoveTask(int min, int hour, bool restart)
 {
     var taskService = new TaskService();
     var task = min + "@" + hour + "@" + ((restart) ? "r" : "s");
     taskService.RootFolder.DeleteTask(@"FOG\" + task);
     taskService.Dispose();
 }
Ejemplo n.º 3
0
        public static bool PersistTask(bool startAtLogon, bool startForAllUsers)
        {
            WinTasks.TaskService ts = new WinTasks.TaskService();
            try
            {
                WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
                bool isElevated = (new WindowsPrincipal(currentIdentity).IsInRole(WindowsBuiltInRole.Administrator));
                WinTasks.Task task = ts.FindTask(Application.ProductName, false);

                if (startAtLogon)
                {
                    if (startForAllUsers && !isElevated)
                    {
                        return PersistTaskElevated(startAtLogon, startForAllUsers);
                    }
                    else
                    {
                        WinTasks.LogonTrigger trigger = (WinTasks.LogonTrigger)WinTasks.LogonTrigger.CreateTrigger(WinTasks.TaskTriggerType.Logon);
                        trigger.Enabled = true;
                        trigger.StartBoundary = DateTime.Today;
                        if (startForAllUsers)
                            trigger.UserId = null;
                        else
                            trigger.UserId = currentIdentity.Name;

                        WinTasks.ExecAction action = (WinTasks.ExecAction)WinTasks.ExecAction.CreateAction(WinTasks.TaskActionType.Execute);
                        action.Path = Application.ExecutablePath;
                        action.WorkingDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

                        if (task == null)
                        {
                            task = ts.AddTask(Application.ProductName, trigger, action, currentIdentity.Name);
                        }
                        else
                        {
                            task.Definition.Triggers.Clear();
                            task.Definition.Triggers.Add(trigger);
                            task.Definition.Actions.Clear();
                            task.Definition.Actions.Add(action);
                            task.RegisterChanges();
                        }
                    }
                }
                else if (task != null)
                {
                    ts.GetFolder("\\").DeleteTask(task.Name);
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                ts.Dispose();
            }
            return true;
        }
Ejemplo n.º 4
0
        public static ActionResult CleanTasks(Session session)
        {
            try
            {
                var taskService = new TaskService();
                var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList();

                foreach (var task in existingTasks)
                    taskService.RootFolder.DeleteTask(@"FOG\" + task.Name);

                taskService.RootFolder.DeleteFolder("FOG", false);
                taskService.Dispose();
            }
            catch (Exception)
            {
                // ignored
            }

            return ActionResult.Success;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            string dir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            string svc = Path.Combine(dir, "ConnectifyService.exe");
            string cli = Path.Combine(dir, "Connectify.exe");
            bool isService = false;
            if (File.Exists(svc) && File.Exists(cli)) {
                RegistryKey runKey = null;
                try {
                    // Remove the client autorun
                    runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
                    runKey.DeleteValue("Connectify", false);

                } catch (Exception) {
                } finally {
                    if (runKey != null)
                        runKey.Close();
                }

                TaskService ts = null;
                try {
                    // Remove service scheduled task if it exists
                    ts = new TaskService();
                    foreach (Task t in ts.RootFolder.Tasks) {
                        if (t.Name.StartsWith("Connectify")) {
                            ts.RootFolder.DeleteTask(t.Name);
                            break;
                        }
                    }
                } catch (Exception) {
                } finally {
                    if (ts != null)
                        ts.Dispose();
                }

                ServiceController service = null;
                ManagementObject classInstance = null;
                ManagementBaseObject inParams = null;
                try {
                    // Set the service to run manually if it exists
                    service = new ServiceController("Connectify");
                    if (service != null) {
                        isService = true;
                        classInstance = new ManagementObject("root\\CIMV2", "Win32_Service.Name='Connectify'", null);
                        inParams = classInstance.GetMethodParameters("ChangeStartMode");
                        inParams["StartMode"] = "Manual";
                        classInstance.InvokeMethod("ChangeStartMode", inParams, null);
                    }
                } catch (Exception) {
                } finally {
                    if (inParams != null)
                        inParams.Dispose();
                    if (classInstance != null)
                        classInstance.Dispose();
                }

                try {
                    if (isService) {
                        // Stop the service if it happens to be running
                        service.Stop();
                    } else {
                        // Kill services if they happen to be running
                        foreach (Process p in Process.GetProcessesByName("ConnectifyService")) {
                            p.Kill();
                            p.WaitForExit();
                        }
                        foreach (Process p in Process.GetProcessesByName("Connectifyd")) {
                            p.Kill();
                            p.WaitForExit();
                        }
                    }
                } catch (Exception) {
                }

                Process daemon = null;
                try {
                    if (isService) {
                        // Start the service
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running);
                    } else {
                        // Launch the service & client
                        daemon = Process.Start(svc);
                        daemon.WaitForInputIdle();
                    }
                } catch (Exception) {
                }

                // Launch the client
                Process client = Process.Start(cli);

                // Wait for client to quit
                client.WaitForExit();

                try {
                    if (isService) {
                        // Stop the service
                        service.Stop();
                    } else {
                        // Kill services
                        daemon.Kill();
                        daemon.WaitForExit();
                        foreach (Process p in Process.GetProcessesByName("Connectifyd")) {
                            p.Kill();
                        }
                    }
                } catch (Exception) {
                } finally {
                    if (service != null)
                        service.Dispose();
                }
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 關閉連線
 /// </summary>        
 private static void CloseTaskService(TaskService ts)
 {
     ts.Dispose();
 }
Ejemplo n.º 7
0
        public void ClearAll()
        {
            var taskService = new TaskService();

            try
            {
                taskService.RootFolder.CreateFolder("FOG");
            }
            catch (Exception)
            {
                // ignored
            }

            var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList();

            foreach (var task in existingTasks)
            {
                Log.Debug(LogName, "Delete task " + task.Name);
                taskService.RootFolder.DeleteTask(@"FOG\" + task.Name);
            }

            taskService.Dispose();
        }