C# 批处理文件写入空文件

C# 批处理文件写入空文件,c#,batch-file,C#,Batch File,我编写单元测试。该代码使用参数调用cmd并将结果写入文件。但结果是这个文件是空的。你能帮我修一下吗 String command = @"tf workspaces /owner:* /computer:* /server:http://servertfs:8080/tfs/Default/ > C:\Test\test1.txt"; ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow

我编写单元测试。该代码使用参数调用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();
}
您可以将设置为true,然后通过获取输出,而不是将>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();
}

您可以将输出放入所需的文件或直接使用它。

什么是tf?它是可执行文件吗?如果是,那么您应该使用tf.exe而不是cmd.exetf它是tfs程序。这是诺拉姆的工作与此命令。但是没有设置输出文件。不幸的是变量输出为空。这是可能的,因为程序不使用标准流。如果命令tf workspace/owner://computer://server:http://servertfs:8080/tfs/Default/ >C:\Test\test1.txt在控制台中执行?将cmd.exe更改为tf.exe并从arguments.No中删除tf。这种方法也会给出空结果。具体哪种方法?从控制台执行?