【发布时间】:2013-01-08 15:41:25
【问题描述】:
我正在尝试使用 System.Diagnostics.Process 从 .net/c# 运行批处理文件。不知何故,它不执行批处理文件的 xcopy 命令。
示例批处理文件:
#copy test to test2 including sub directories
xcopy c:\test\ c:\test2
C#代码:
public void RunMSIBatchFile(string _workingDirectory, string batchFileName)
{
var process = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
WorkingDirectory = _workingDirectory,
FileName = _workingDirectory + batchFileName,
CreateNoWindow = true,
RedirectStandardError = true
}
};
process.OutputDataReceived += ProcessOutputDataReceived;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit(Convert.ToInt32(CommandTimeOut.TotalMilliseconds));
}
如果我将 UseShellExecute 更改为 true,那么它可以工作,但似乎无法捕获标准输出。
有人遇到过这样的问题吗?
【问题讨论】:
-
XCopy 很特别。当您仅重定向其输出时,它将无法正常工作。您还必须重定向输入。
-
您好,重定向标准输入已解决问题。非常感谢:)
-
原来在我们的情况下重定向 STDIN 也是解决这个问题的方法。如果不重定向 STDIN,xcopy 将返回在 STDERR 上找不到的文件,这几乎没有意义。所有其他标准实用程序似乎都可以正常工作。汉斯——或许可以单独回答这个问题?
标签: c# batch-file xcopy