3

I'm currently trying to read the outputstream from an external console application. I can not modify this application.

This is to code i'm currently using:

        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

I use this to read the outputstream:

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

But whenever I add:

        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)

This doesn't make sense because this is a console application? How can I fix this problem? Or is there a simple work around?

Edit: Tried this: 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);
    } 

Still didnt work :s

Community
  • 1
  • 1
user2997204
  • 1,344
  • 2
  • 12
  • 24
  • can you show the code that reads the `StandardOutput` ? – Khanh TO Nov 08 '15 at 03:02
  • could it be that there is an error when executing `volibot.exe`? or the path to `volibot.exe` is incorrect – Khanh TO Nov 08 '15 at 03:24
  • Volibot runs perfect from the commandline. – user2997204 Nov 08 '15 at 03:28
  • From what I read here: https://communities.ca.com/thread/241697832. Maybe you can try: `process.StartInfo.CreateNoWindow = true;`. Not sure, though – Khanh TO Nov 08 '15 at 03:29
  • I guess the code inside `volibot.exe` uses some environment information like [workingdirectory](https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory(v=vs.110).aspx), but these kind of information is not provided in your code, causing volibot to run into errors. When run from commandline, these information is provided by the shell – Khanh TO Nov 08 '15 at 03:34
  • Do you maybe know any other ways to get this output data? @KhanhTO – user2997204 Nov 08 '15 at 03:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94514/discussion-between-khanh-to-and-user2997204). – Khanh TO Nov 08 '15 at 03:51

2 Answers2

1

Try something like:

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)
            {

            }
        }
    }
dean
  • 81
  • 1
  • 6
0

You have @"C:\Users\pc\Documents...." you don't need double \ when you use the @ sign..

maybe it is that?

dean
  • 81
  • 1
  • 6