我写单元测试。代码使用参数调用cmd并将结果写入文件。但结果这是文件是空的。你能帮我解决一下吗?
String command = @"tf workspaces /owner:* /computer:* /server:http://servertfs:8080/tfs/Default/ > C:\Test\test1.txt";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
答案 0 :(得分:0)
您可以将ProcessStartInfo.RedirectStandardOutput
设置为true
,然后通过Process.StandardOutput
获取输出,而不是将> C:\Test\test1.txt
附加到命令中。
String command = @"tf workspaces /owner:* /computer:* /server:http://servertfs:8080/tfs/Default/";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command;
startInfo.RedirectStandardOutput = true;
using (Process exeProcess = Process.Start(startInfo))
{
string output = exeProcess.StandardOutput.ReadToEnd();
exeProcess.WaitForExit();
}
您可以将输出放到所需的文件中或直接使用它。