Reputation: 1384
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
Upvotes: 3
Views: 519
Reputation: 81
You have @"C:\Users\pc\Documents...." you don't need double \ when you use the @ sign..
maybe it is that?
Upvotes: 0
Reputation: 81
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)
{
}
}
}
Upvotes: 1