示例#1
0
        /// <summary>
        /// Creates/overwrites the scheduled task for the specified mirror task.
        /// </summary>
        public Task Save(MirrorTask mirrorTask, Trigger trigger)
        {
            if (mirrorTask == null)
            {
                throw new ArgumentNullException("mirrorTask");
            }
            if (trigger == null)
            {
                throw new ArgumentNullException("trigger");
            }

            var definition = _service.NewTask();

            definition.RegistrationInfo.Description = string.Format("Mirrors {0} to {1}.",
                                                                    PathHelper.Quote(mirrorTask.Source), PathHelper.Quote(mirrorTask.Target));

            definition.Actions.Add(new ExecAction(Application.ExecutablePath,
                                                  mirrorTask.Guid, Application.StartupPath));

            definition.Triggers.Add(trigger);

            definition.Principal.LogonType = TaskLogonType.InteractiveToken;

            // set some advanced settings under Vista+
            if (!_v1Mode)
            {
                definition.Settings.AllowHardTerminate     = false;
                definition.Settings.StopIfGoingOnBatteries = false;
                definition.Settings.StartWhenAvailable     = true;

                if (UacHelper.IsInAdminRole())
                {
                    definition.Principal.RunLevel = TaskRunLevel.Highest;
                }
            }

            Delete(mirrorTask);

            return(_folder.RegisterTaskDefinition(GetName(mirrorTask), definition,
                                                  TaskCreation.Create, null, null, TaskLogonType.InteractiveToken, null));
        }