Exemplo n.º 1
0
        public static bool CheckIfAdminLaunchTaskExist(string appTaskId = "")
        {
            try
            {
                if (appTaskId == null || appTaskId.Length == 0)
                {
                    string exeName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    appTaskId = "Run" + exeName + "InAdminMode";
                }

                tsEngine.TaskScheduler ts = new tsEngine.TaskScheduler();
                ts.Connect(null, null, null, null);
                if (ts.Connected)
                {
                    tsEngine.ITaskFolder     root = ts.GetFolder("\\");
                    tsEngine.IRegisteredTask task = root.GetTask(appTaskId);
                    if (task != null)
                    {
                        return(true);
                    }
                }
            }
            catch { }
            return(false);
        }
Exemplo n.º 2
0
 public static void StartWithAdminModeLaunchTask(string appTaskId = "")
 {
     if (appTaskId == null || appTaskId.Length == 0)
     {
         string exeName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
         appTaskId = "Run" + exeName + "InAdminMode";
     }
     if (CheckIfAdminLaunchTaskExist(appTaskId))
     {
         //if (!IsAdminLaunchTaskAlreadyRunning())
         {
             tsEngine.TaskScheduler ts = new tsEngine.TaskScheduler();
             ts.Connect(null, null, null, null);
             if (ts.Connected)
             {
                 tsEngine.ITaskFolder     root    = ts.GetFolder("\\");
                 tsEngine.IRegisteredTask task    = root.GetTask(appTaskId);
                 tsEngine.IRunningTask    runTask = task.Run(null);
             }
         }
         //else
         //{
         //    throw new Exception("The task is already running!");
         //}
     }
     else
     {
         throw new Exception("Admin mode Launch task does not exist yet!");
     }
 }
Exemplo n.º 3
0
 public static void DeleteAdminLaunchTask(string appTaskId = "")
 {
     if (appTaskId == null || appTaskId.Length == 0)
     {
         string exeName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
         appTaskId = "Run" + exeName + "InAdminMode";
     }
     if (IsInAdminMode())
     {
         if (CheckIfAdminLaunchTaskExist(appTaskId))
         {
             tsEngine.TaskScheduler ts = new tsEngine.TaskScheduler();
             ts.Connect(null, null, null, null);
             if (ts.Connected)
             {
                 tsEngine.ITaskFolder     root = ts.GetFolder("\\");
                 tsEngine.IRegisteredTask task = root.GetTask(appTaskId);
                 if (task != null)
                 {
                     root.DeleteTask(appTaskId, 0);
                 }
             }
         }
     }
     else
     {
         throw new Exception("To delete the admin mode task you must start this program in 'Admin' mode.");
     }
 }
Exemplo n.º 4
0
        public TaskSchedulerWrapper()
        {
            mService = new S.TaskScheduler();
            mService.Connect();

            mRootFolder = mService.GetFolder(@"\");
        }
Exemplo n.º 5
0
        public void Dispose()
        {
            if (mService != null)
            {
                Marshal.ReleaseComObject(mService);
                mService = null;
            }

            if (mRootFolder != null)
            {
                Marshal.ReleaseComObject(mRootFolder);
                mRootFolder = null;
            }
        }
Exemplo n.º 6
0
        public static void CreateAdminLaunchTask(string appTaskId = "")
        {
            if (appTaskId == null || appTaskId.Length == 0)
            {
                string exeName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
                appTaskId = "Run" + exeName + "InAdminMode";
            }
            if (IsInAdminMode())
            {
                tsEngine.TaskScheduler ts = new tsEngine.TaskScheduler();
                ts.Connect(null, null, null, null);
                if (ts.Connected)
                {
                    tsEngine.ITaskDefinition task = ts.NewTask(0);
                    task.RegistrationInfo.Author      = "Mine";
                    task.RegistrationInfo.Description = appTaskId;
                    task.Principal.RunLevel           = tsEngine._TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
                    task.Settings.MultipleInstances   = tsEngine._TASK_INSTANCES_POLICY.TASK_INSTANCES_PARALLEL;
                    tsEngine.ITimeTrigger trigger = (tsEngine.ITimeTrigger)task.Triggers.Create(tsEngine._TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
                    trigger.Id            = "NoTime";
                    trigger.StartBoundary = "2000-01-01T12:00:00";
                    trigger.StartBoundary = "2000-01-01T12:00:00";
                    tsEngine.IExecAction action = (tsEngine.IExecAction)task.Actions.Create(tsEngine._TASK_ACTION_TYPE.TASK_ACTION_EXEC);
                    action.Id               = "Run exe";
                    action.Path             = System.Reflection.Assembly.GetExecutingAssembly().Location;
                    action.WorkingDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

                    tsEngine.ITaskFolder     root    = ts.GetFolder("\\");
                    tsEngine.IRegisteredTask regTask = root.RegisterTaskDefinition(
                        appTaskId,
                        task,
                        (int)tsEngine._TASK_CREATION.TASK_CREATE_OR_UPDATE,
                        null,
                        null,
                        tsEngine._TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN);
                }
            }
            else
            {
                throw new Exception("To create the admin mode task you must start this program in 'Admin' mode.");
            }
        }