如何将控制台输出重定向?

3

我目前正在尝试从外部控制台应用程序读取输出流。我无法修改此应用程序。

这是我目前使用的代码:

        Process process = new Process();
        string dir = Directory.GetCurrentDirectory();
        process.StartInfo.FileName = dir + "/volibot.exe";
        process.StartInfo.UseShellExecute = false;
        process.Start();
        //do some stuff with the stream

我使用这个来读取outputstream:

            while (!process.StandardOutput.EndOfStream)
            {
                string message = process.StandardOutput.ReadLine();
                Console.WriteLine(message);
            }

但是每当我添加以下内容时:
        process.StartInfo.RedirectStandardOutput = true;

I get this error:

Unhandled exception: System.IO.IOException: The handle is invalid.

   bij System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   bij System.IO.__Error.WinIOError()
   bij System.Console.SetWindowSize(Int32 width, Int32 height)
   bij RitoBot.Core.Main(String[] args)

这不合理,因为这是一个控制台应用程序?我该如何解决这个问题?还是有简单的解决方法吗?
编辑: 尝试了这个:ProcessInfo and RedirectStandardOutput
    static void Main(string[] args)
    {
        //C:\\Users\\pc\\Documents\\Visual Studio 2013\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\VoliBot.exe
        Process build = new Process();
        build.StartInfo.WorkingDirectory = @"C:\\Users\\pc\\Documents\\Visual Studio 2013\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\";
        build.StartInfo.Arguments = "";
        build.StartInfo.FileName = "volibot.exe";

        build.StartInfo.UseShellExecute = false;
        build.StartInfo.RedirectStandardOutput = true;
        build.StartInfo.RedirectStandardError = true;
        build.StartInfo.CreateNoWindow = true;
        build.ErrorDataReceived += build_ErrorDataReceived;
        build.OutputDataReceived += build_ErrorDataReceived;
        build.EnableRaisingEvents = true;
        build.Start();
        build.BeginOutputReadLine();
        build.BeginErrorReadLine();
        build.WaitForExit();

        Console.ReadLine();

    }

    static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        string strMessage = e.Data;
        Console.WriteLine(strMessage);
    } 

仍然无法工作 :s

你能展示读取 StandardOutput 的代码吗? - Khanh TO
执行 volibot.exe 时是否出现错误?或者 volibot.exe 的路径不正确。 - Khanh TO
Volibot可以在命令行中完美运行。 - user2997204
从我在这里阅读的内容:https://communities.ca.com/thread/241697832。也许你可以尝试:`process.StartInfo.CreateNoWindow = true;`。不过我不确定。 - Khanh TO
我猜测volibot.exe内部的代码使用了一些环境信息,例如workingdirectory,但这些信息在你的代码中没有提供,导致volibot运行时出现错误。当从命令行运行时,这些信息由shell提供。 - Khanh TO
显示剩余2条评论
2个回答

1
尝试类似以下的内容:
var psi = new ProcessStartInfo(@"c:\temp\mycommand.exe", String.Format(" \"{0}\" \"{1}\" \"{2}\"", sourceDoc, dataSource, outputFile))
        {
            WorkingDirectory = Environment.CurrentDirectory,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = false
        };

        using (var process = new Process { StartInfo = psi })
        {
            process.Start();
             process.BeginErrorReadLine();
              process.BeginOutputReadLine();

            // wait for the process to exit
            process.WaitForExit();


            if (process.ExitCode != 0)
            {

            }
        }
    }

它仍然给我相同的崩溃 :( - user2997204

0

你有 @"C:\Users\pc\Documents....",当你使用@符号时,不需要双斜杠。

也许就是这个原因?


1
很遗憾,不是那样的 :( - user2997204

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接