c#中的Dos命令-重定向标准错误/输出问题
我需要在类中运行Dos命令。我的问题是重定向选项似乎阻止命令运行。c#中的Dos命令-重定向标准错误/输出问题,c#,.net,C#,.net,我需要在类中运行Dos命令。我的问题是重定向选项似乎阻止命令运行。 这是我的密码: public static int executeCommand(string cmd) { System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C " + cmd); int exitCode = 0;
这是我的密码:
public static int executeCommand(string cmd)
{
System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C " + cmd);
int exitCode = 0;
//processStartInfo.RedirectStandardError = true;
//processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
System.Diagnostics.Process process =
System.Diagnostics.Process.Start(processStartInfo);
process.WaitForExit(); //wait for 20 sec
exitCode = process.ExitCode;
//string stdout = process.StandardOutput.ReadToEnd();
//string stderr = process.StandardError.ReadToEnd();
process.Close();
return exitCode;
}
当我调用xcopy时:
if (executeCommand("xcopy.exe " + "/E /I /R /Y /Q c:\\temp\\*.* e:\\temp\\b1\\ ") != 0)
Log.Error("Error detected running xcopy ");
该方法正确地运行xcopy。如果我想重定向SDTOUT和STDERR,该方法也会返回0,但xcopy并没有真正运行
换句话说,这是行不通的:
public static int executeCommand(string cmd)
{
System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo("CMD.exe", "/C " + cmd);
int exitCode = 0;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
System.Diagnostics.Process process =
System.Diagnostics.Process.Start(processStartInfo);
process.WaitForExit(); //wait for 20 sec
exitCode = process.ExitCode;
string stdout = process.StandardOutput.ReadToEnd();
string stderr = process.StandardError.ReadToEnd();
process.Close();
return exitCode;
}
知道为什么吗
谢谢
tony这是xcopy.exe的怪癖,您还必须重定向stdin。检查我的原始诊断。顺便说一句,不需要使用cmd.exe,只需直接调用xcopy。这是xcopy.exe的怪癖,您还必须重定向stdin。检查我的原始诊断。顺便说一句,不需要使用cmd.exe,直接调用xcopy即可。就是这样!!非常感谢。上面的代码和我的代码现在都可以工作了。我注意到其他命令执行正确。只是xcopy没有工作。谢谢你,就是这样!!非常感谢。上面的代码和我的代码现在都可以工作了。我注意到其他命令执行正确。只是xcopy没有工作。谢谢你,托尼