protected override void OnExecute() { // Check if file can be overwritten if (File.Exists(Destination)) { if (!Overwrite) { throw new IOException(ExceptionMessages.FileAlreadyExists); } else { File.Delete(Destination); } } // Create destination folder if (!String.IsNullOrEmpty(Path.GetDirectoryName(destination)) && !Directory.Exists(Path.GetDirectoryName(destination))) { Directory.CreateDirectory(Path.GetDirectoryName(destination)); } // Figure out the working directory from the service's exe var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); // Execute eseutil to perform copy var info = new ProcessStartInfo( Path.Combine(path, "eseutil.exe"), String.Format("/y \"{0}\" /d \"{1}\"", source, destination)); // These are important to run program under the delegated account info.UseShellExecute = false; info.CreateNoWindow = true; var guid = Guid.NewGuid(); var cproc = new CancelableProcess(info); RegisterCancelable(guid, cproc); cproc.Execute(); UnregisterCancelable(guid); if (cproc.IsCanceled || cproc.ExitCode == -1073741510) { throw new OperationCanceledException(ExceptionMessages.FileCopyCanceled); } else if (cproc.ExitCode > 0) { throw new Exception(String.Format(ExceptionMessages.FileCopyFailed, cproc.ExitCode)); } // rename destination file if file names differ // **** TODO: test this /* delete if (StringComparer.InvariantCultureIgnoreCase.Compare(Path.GetDirectoryName(source), Path.GetDirectoryName(destination)) != 0 && StringComparer.InvariantCultureIgnoreCase.Compare(Path.GetFileName(source), Path.GetFileName(destination)) != 0) { string from = Path.Combine(Path.GetDirectoryName(destination), Path.GetFileName(source)); Console.WriteLine("{0} -> {1}", from, source); File.Move(from, destination); }*/ }