Example #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));
        }
Example #2
0
        /// <summary>Builds the command-line switches for Robocopy.</summary>
        private static string BuildSwitches(MirrorTask task, string sourceFolder)
        {
            string basicSwitches = string.IsNullOrEmpty(task.CustomRobocopySwitches)
                                ? Properties.Settings.Default.RobocopySwitches
                                : task.CustomRobocopySwitches;

            // if supported, use Robocopy's backup mode to avoid access denied errors
            if (UacHelper.IsRobocopyBackupModeSupported())
            {
                // do the basic switches include restartable mode (/z)?
                int zIndex = basicSwitches.IndexOf("/z ", StringComparison.OrdinalIgnoreCase);
                if (zIndex < 0 && basicSwitches.EndsWith("/z", StringComparison.OrdinalIgnoreCase))
                {
                    zIndex = basicSwitches.Length - 2;
                }

                // if so, change /z to /zb to enable restartable mode with backup mode fallback on access denied,
                // else add /b for normal backup mode
                if (zIndex >= 0)
                {
                    basicSwitches = basicSwitches.Substring(0, zIndex) + "/zb" + basicSwitches.Substring(zIndex + 2);
                }
                else
                {
                    basicSwitches += " /b";
                }
            }

            var switches = new StringBuilder();

            switches.Append(basicSwitches);

            if (!string.IsNullOrEmpty(task.ExtendedAttributes))
            {
                switches.Append(" /copy:dat");
                switches.Append(task.ExtendedAttributes);
            }

            if (task.DeleteExtraItems)
            {
                switches.Append(" /purge");
            }

            if (!task.OverwriteNewerFiles)
            {
                switches.Append(" /xo");                 // exclude older files in the source folder
            }
            if (!string.IsNullOrEmpty(task.ExcludedAttributes))
            {
                switches.Append(" /xa:");
                switches.Append(task.ExcludedAttributes);
            }

            if (task.ExcludedFiles.Count > 0)
            {
                switches.Append(" /xf");
                foreach (string file in task.ExcludedFiles)
                {
                    AppendPathOrWildcard(switches, sourceFolder, file);
                }
            }

            if (task.ExcludedFolders.Count > 0)
            {
                switches.Append(" /xd");
                foreach (string folder in task.ExcludedFolders)
                {
                    AppendPathOrWildcard(switches, sourceFolder, folder);
                }
            }

            return(switches.ToString());
        }