public static string RoboCopyDirectory(DirectoryInfo sourceDir, DirectoryInfo targetDir) { // Mirror NoProgress Wait 0 Retry 0 var arguments = new List <string> { "/MIR", "/NP", "/W:0", "/R:0", ProcessUtility.EncodeArgumentForCommandLine(sourceDir.FullName), ProcessUtility.EncodeArgumentForCommandLine(targetDir.FullName) }; var processUtilityResult = ProcessUtility.ShellAndWaitImpl(sourceDir.Parent.FullName, "robocopy.exe", arguments, true, null); var robocopySucceeded = (processUtilityResult.ReturnCode < 4); var exitCodeEvaluation = robocopySucceeded ? "Robocopy OK" : "Robocopy ERROR"; var outputAndExitCode = String.Format("{2}\r\n{0}\r\nExit Code: {1} {2}", processUtilityResult.StdOutAndStdErr, processUtilityResult, exitCodeEvaluation); if (!robocopySucceeded) { throw new Exception(outputAndExitCode); } return(outputAndExitCode); }
/// <summary> /// Convert Html page at a given URL to a PDF file using open-source tool wkhtml2pdf /// </summary> // ReSharper disable once InconsistentNaming public static void ConvertURLToPDF(Uri url, FileInfo outputFile, PdfConversionSettings settings) { Check.RequireNotNull(settings); var exeInfo = new FileInfo(SitkaConfiguration.GetRequiredAppSetting(settings.AppKeyExecPath)); Check.RequireFileExists(exeInfo); Check.RequireDirectoryExists(outputFile.DirectoryName); var commandLineArguments = settings.BuildCommandSwitches(); commandLineArguments.Add(url.ToString()); commandLineArguments.Add(outputFile.FullName); var result = ProcessUtility.ShellAndWaitImpl(exeInfo.DirectoryName, exeInfo.FullName, commandLineArguments, true, Convert.ToInt32(settings.ExecutionTimeout.TotalMilliseconds)); var retCode = result.ReturnCode; var argumentsAsString = String.Join(" ", commandLineArguments.Select(ProcessUtility.EncodeArgumentForCommandLine).ToList()); var fullProcessAndArguments = $"{ProcessUtility.EncodeArgumentForCommandLine(exeInfo.FullName)} {argumentsAsString}"; Check.Ensure(retCode == 0, $"Process {exeInfo.Name} returned with exit code {retCode}, expected exit code 0.\r\nProcess Command Line:\r\n{fullProcessAndArguments}\r\nProcess Working Directory: {exeInfo.DirectoryName}\r\nStdErr and StdOut:\r\n{result.StdOutAndStdErr}"); }
public void Test() { Assert.That(ProcessUtility.EncodeArgumentForCommandLine("A"), Is.EqualTo("A"), "Easy stuff should just go straight across"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine("With Space"), Is.EqualTo("\"With Space\""), "Space should get double quoted"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine("With\tTab"), Is.EqualTo("\"With\tTab\""), "Tab should get double quoted"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine("With\nLF"), Is.EqualTo("\"With\nLF\""), "LF should get double quoted"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine("With\rCR"), Is.EqualTo("\"With\rCR\""), "CR should get double quoted"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine(@"BackslashAtEndNoSpacesNoBigDeal\"), Is.EqualTo(@"BackslashAtEndNoSpacesNoBigDeal\"), "Backslash and quotes get tricky"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine(@"Backslash at end with spaces get funky\"), Is.EqualTo(@"""Backslash at end with spaces get funky\\"""), "Backslash and quotes get tricky"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine(@"a\""b"), Is.EqualTo("a\\\"b"), "Backslash and quotes get tricky - backslashes before quotes have to be doubled up, plus one blackslash to mark the quote as literal"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine(@"a\\b c"), Is.EqualTo(@"""a\\b c"""), "Backslash and quotes get tricky"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine(@"C:\My Path\"), Is.EqualTo(@"""C:\My Path\\"""), "Backslash and quotes get tricky"); Assert.That(ProcessUtility.EncodeArgumentForCommandLine(@"This is \""My Inner Quote\"" with backslashes"), Is.EqualTo(@"""This is \\\""My Inner Quote\\\"" with backslashes"""), "Backslash and quotes get tricky"); }