我目前正在通过调用xcopy传递 System.Diagnostics.Process 类来递归复制目录 .

这很有效,直到 tried to redirect the standard output 通过 System.Diagnostics.ProcessStartInfo.RedirectStandardOutput 参数 . 它没有在控制台中打印任何内容 and caused the copy to fail.

为什么单独使用此参数 System.Diagnostics.ProcessStartInfo.RedirectStandardOutput 足以使命令在设置为true时失败?

是因为它还重定向所有输出,如文件描述符?

我真的不明白这个问题 . 这是我的代码:

public string ExecuteCommandSync(WinProcParams parameters)
{
    try
    {
        ProcessStartInfo procStartInfo;
        Console.WriteLine("[PROC][EXEC] " + parameters.executable + " : " + parameters.command);

        // If it's a call to cmd, /c tells cmd that we want this to be executed and then exit
        if (parameters.executable == "cmd")
            procStartInfo = new ProcessStartInfo(parameters.executable, "/c " + parameters.command);
        else
            procStartInfo = new ProcessStartInfo(parameters.executable, parameters.command);
        procStartInfo.RedirectStandardOutput = false; //true
        procStartInfo.UseShellExecute = false;
        procStartInfo.ErrorDialog = false;
        procStartInfo.CreateNoWindow = true;

        Process proc = new Process();
        proc.StartInfo = procStartInfo;
        if (!proc.Start())
            Console.WriteLine("[PROC][ERROR] Command failed to launch!");
        string result = proc.StandardOutput.ReadToEnd();
        Console.WriteLine("[PROC][STDOUT] " + result);

        proc.WaitForExit();
    }
    catch (Exception objException)
    {
        return objException.ToString();
    }
    return "";
}

RedirectStandardOutput 设置为 true 时,它将打印"[PROC][STDOUT] "行 . 在另一种情况下,它没有 .

这是设置为true时的输出:

[PROC][EXEC] cmd : %windir%\system32\xcopy.exe "C:\Users\Quentin\Documents\Toto" "C:\Users\Quentin\Documents\Toto_7-9-2014_16-58-17" /H /J /E /C /R /I /K /Y
Le thread '<Sans nom>' (0x1acc) s'est arrêté avec le code 0 (0x0).
[PROC][STDOUT]