I have an app which starts another app. This other app prints a few lines into the Console but noone needs this output and it prints it's output betwenn my own. How can I prevent this other app from printing it's stuff into my console?
I tried to run with ProcessStartInfo.UseShellExecutive both on true and false, also tried to change the console output into a MemoryStream before starting but since I need the Console i had to change the output back and it looks like the other app got their input changed back too.
Process serverprocess = new Process();
serverprocess.StartInfo.FileName = Path.GetFileName(serverpath);
serverprocess.StartInfo.Arguments = launch;
serverprocess.StartInfo.UseShellExecute = false;
serverprocess.StartInfo.RedirectStandardOutput = true;
serverprocess.Start();
In your code ensure that you are re-directing both StandardOutput and StandardError that way everything that the "ThirdPartyApp" writes will be captured in either of these streams.
I have written a small Helper Class that helps with this
You can use like
//Launching excel.exe with /safe as arg
var excelExample1 = #"""C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE"" /safe";
LaunchCMD.Invoke(excelExample1);
//To get its output, if any
var getOutput = LaunchCMD.Output;
LaunchCMD Helper Class
class LaunchCMD
{
public static string Output
{
get; set;
} = "";
public static void Invoke(string command, bool waitTillExit = false, bool closeOutputWindow = false)
{
ProcessStartInfo ProcessInfo;
Process Process = new Process();
ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
ProcessInfo.CreateNoWindow = false;
ProcessInfo.UseShellExecute = false;
ProcessInfo.RedirectStandardOutput = true;
ProcessInfo.RedirectStandardError = true;
Process.EnableRaisingEvents = true;
Process = Process.Start(ProcessInfo);
Process.ErrorDataReceived += ConsoleDataReceived;
Process.OutputDataReceived += ConsoleDataReceived;
Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
if (waitTillExit == true)
{
Process.WaitForExit();
}
if (closeOutputWindow == true)
{
Process.CloseMainWindow();
}
Process.Close();
System.Threading.Thread.Sleep(1000);
Output.ToString();
}
private static void ConsoleDataReceived(object sender, DataReceivedEventArgs e)
{
System.Threading.Thread.Sleep(1000);
if (e.Data != null)
{
Output = Output + e.Data;
}
}
}
I am trying to execute a process exactly as if it were executed on the window's command line but the process class won't allow the executable to create a file similar to what it would do if it were run from the command line. The Processes will when deployed run asynchronously on a server at timed intervals. The command line would calls would look like this:
curl.exe url -o data
wgrib2.exe data -csv output.csv
In the code below I found a workaround for curl.exe but when it is read as StandardOutput the file cuts off some of the critical stopping characters for this file type.
public static void httpQuery(string queryString)
{
Process myProcess = new Process();
try
{
string basepath = AppDomain.CurrentDomain.BaseDirectory;
myProcess.StartInfo.UseShellExecute = false; // allows us to redirect the output
myProcess.StartInfo.CreateNoWindow = true; // stop's command window from being made
myProcess.StartInfo.Verb = "runas";
myProcess.StartInfo.FileName = #"C:\Users\Ian's\Desktop\wgrib2\curl.exe";
myProcess.StartInfo.Arguments = queryString; // queryString is http://nomads.noaa.gov/....
myProcess.StartInfo.RedirectStandardError = true; // redirect error to stream reader
myProcess.StartInfo.RedirectStandardOutput = true; // redirect output to strem reader
myProcess.Start();
if (myProcess.WaitForExit(10000) == false)
{
myProcess.Kill();
}
using(StreamReader reader = myProcess.StandardError) // capture error output if any
{
string result = reader.ReadToEnd();
reader.Close();
StreamReader message = myProcess.StandardOutput;
string output = message.ReadToEnd();
StreamWriter writer = new StreamWriter(basepath + "wgrib2\\data");
writer.Write(output);
writer.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
myProcess.Close();
}
Additionally, I cannot seem to find a way to allow the second process to write a new file, or if it has been I cannot find the directory that it is writing to. I am working with VS2012 on the IIS development server. This is the second processes code.
public static void callWgrib2()
{
Process wgrib2 = new Process();
try
{
// call on command line: wgrib2.exe data -csv data.csv
string basepath = AppDomain.CurrentDomain.BaseDirectory; // path to project directory
string arg1 = basepath + "wgrib2\\data"; // filename of operate on
string arg2 =" -csv " + basepath + "wgrib2\\data.csv"; // filename to write to
wgrib2.StartInfo.UseShellExecute = false; // parameters
wgrib2.StartInfo.CreateNoWindow = true;
wgrib2.StartInfo.Verb = "runas";
wgrib2.StartInfo.FileName = #"C:\Users\Ian's\Desktop\wgrib2\wgrib2.exe";
wgrib2.StartInfo.Arguments = "\""+ arg1 + arg2 + "\"";
wgrib2.StartInfo.RedirectStandardError = true;
wgrib2.Start();
if (wgrib2.WaitForExit(10000) == false)
{
wgrib2.Kill();
}
using (StreamReader reader = wgrib2.StandardOutput)
{
string result = reader.ReadToEnd();
reader.Close();
StreamReader err = wgrib2.StandardError;
err.ReadToEnd();
err.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
wgrib2.Close();
}
Can someone point me in the write direction I have put considerable effort into this problem but I'm sure there exits a simple workaround I'm not aware of.
If you want to use the actual command line process, you can use cmd and pass the /c flag to it in the arguements
also, the line
wgrib2.StartInfo.Arguments = "\""+ arg1 + arg2 + "\"";
looks odd
do you really need all your arguments such as -csv to be in the same quotes block?
it's like calling on the command line
C:\Users\Ian's\Desktop\wgrib2\wgrib2.exe "c:\base\wgrib2\data -csv c:\base\wgrib2\data.csv"
I wrote a small program in C# NET a while back to keep a Java process running. I am about to deploy it to a bunch of servers and I am working on fixing up some of the code now. As it stands, I don't think I have this setup right.
What would be the best way to keep the process running that I am creating in my LaunchMinecraft() function? I want to have it so as long as my process is running, it continues to restart this process if it crashes.
static void Main(string[] args)
{
// Launch the Application
LaunchMinecraft("minecraft_server.jar", "512");
}
public static void LaunchMinecraft(string file, string memory)
{
string memParams = "-Xms" + memory + "M" + " -Xmx" + memory + "M ";
string args = memParams + "-jar " + file + " nogui";
ProcessStartInfo processInfo = new ProcessStartInfo("java.exe", args);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardInput = true;
processInfo.RedirectStandardError = true;
try
{
using (Process minecraftProcess = Process.Start(processInfo))
{
// Store Process Globally to access elsewhere
JavaProcess = minecraftProcess;
// Creates a Repeating Poll Timer (30 Seconds)
// Use this to keep the Minecraft's Process Affinity/Priority the same as the Daemon
PollTimer = new System.Timers.Timer();
PollTimer.Elapsed += new ElapsedEventHandler(Event_PollProcess);
PollTimer.Interval = 5000;
PollTimer.Start();
Console.WriteLine("Minecraft Process Started.");
// Setup Callback for Redirected Output/Error Streams
minecraftProcess.OutputDataReceived += new DataReceivedEventHandler(Handler_ProcessOutput);
minecraftProcess.ErrorDataReceived += new DataReceivedEventHandler(Handler_ProcessError);
// Steam Writer
streamWriter = minecraftProcess.StandardInput;
minecraftProcess.BeginOutputReadLine();
minecraftProcess.BeginErrorReadLine();
while (minecraftProcess.HasExited == false)
{
String strInput = Console.ReadLine();
SendProcessCmd(strInput);
}
minecraftProcess.WaitForExit();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
The Process class itself cant prevent your external program from shutting down, you have to use the Exited event as #Jalal mentiones or poll the HasExited property of the process.
I am reading the output of a java application started using Process and reading stdError, stdOutout and using stdInput to send commands. Here is the relevant code:
int mem = Properties.Settings.Default.mem_max;
string locale = Properties.Settings.Default.location;
Process bukkit_jva = new Process();
bukkit_jva.StartInfo.FileName = "java";
//bukkit_jva.StartInfo.Arguments = "-Xmx" + mem + "M -Xms" + mem + "M -jar " + locale + "bukkit.jar";
bukkit_jva.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\bukkit\\bukkit.jar";
bukkit_jva.StartInfo.UseShellExecute = false;
bukkit_jva.StartInfo.CreateNoWindow = true;
bukkit_jva.StartInfo.RedirectStandardError = true;
bukkit_jva.StartInfo.RedirectStandardOutput = true;
bukkit_jva.StartInfo.RedirectStandardInput = true;
bukkit_jva.Start();
//start reading output
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
SetText(bukkit_jva.StandardOutput.ReadLine());
StreamReader err = bukkit_jva.StandardError;
StreamReader output = bukkit_jva.StandardOutput;
StreamWriter writer = bukkit_jva.StandardInput;
SetText(err.Peek().ToString());
while (false == false)
{
if (vars.input != null)
{
writer.WriteLine(vars.input);
vars.input = null;
}
SetText(output.ReadLine() + err.ReadLine());
}
}
SetText() adds the line to a list of lines.
My problem is that the java app sometimes returns a string even when there is no input, so I always need to check for a new line. but If I need to send a command, and there is no new output, It will not send.
I tried different If statements on the readline, but it would only return the first few lines then it would stop.
basicly it seems to pause the loop if there is no new line for it to read.
How could I either setup my read/write loop differently or get the loop to unpause?
Thanks,
Adam
try this:
static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo("echoApp.exe");
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process echoApp = new Process();
echoApp.ErrorDataReceived += new DataReceivedEventHandler(echoApp_ErrorDataReceived);
echoApp.OutputDataReceived += new DataReceivedEventHandler(echoApp_OutputDataReceived);
echoApp.StartInfo = psi;
echoApp.Start();
echoApp.BeginOutputReadLine();
echoApp.BeginErrorReadLine();
echoApp.StandardInput.AutoFlush = true;
string str = "";
while (str != "end")
{
str = Console.ReadLine();
echoApp.StandardInput.WriteLine(str);
}
echoApp.CancelOutputRead();
echoApp.CancelErrorRead();
echoApp.Close();
}
static void echoApp_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("stdout: {0}", e.Data);
}
static void echoApp_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("stderr: {0}", e.Data);
}
and the little echoApp...
//echoApp
static void Main(string[] args)
{
string str="";
while (str != "end")
{
str = Console.ReadLine();
Console.WriteLine(str);
}
}
Please note if you try to access the output after CancelOutputRead() and CancelErrorRead() you may find that occasionally you are missing some text. I found the flush only occurs after explicitly calling Close(). Disposing (with a using statement) doesn't help.
This symptom will most likely occur when calling the command processor (CMD.EXE) because it does not explicitly flush itself. So be careful not to try and access the output (written from your event handlers) unless you explicitly call Close() first.
How do I execute a command-line program from C# and get back the STD OUT results? Specifically, I want to execute DIFF on two files that are programmatically selected and write the results to a text box.
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "YOURBATCHFILE.bat";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Code is from MSDN.
Here's a quick sample:
//Create process
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = strCommand;
//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = strCommandParameters;
pProcess.StartInfo.UseShellExecute = false;
//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;
//Optional
pProcess.StartInfo.WorkingDirectory = strWorkingDirectory;
//Start the process
pProcess.Start();
//Get program output
string strOutput = pProcess.StandardOutput.ReadToEnd();
//Wait for process to finish
pProcess.WaitForExit();
There one other parameter I found useful, which I use to eliminate the process window
pProcess.StartInfo.CreateNoWindow = true;
this helps to hide the black console window from user completely, if that is what you desire.
// usage
const string ToolFileName = "example.exe";
string output = RunExternalExe(ToolFileName);
public string RunExternalExe(string filename, string arguments = null)
{
var process = new Process();
process.StartInfo.FileName = filename;
if (!string.IsNullOrEmpty(arguments))
{
process.StartInfo.Arguments = arguments;
}
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
var stdOutput = new StringBuilder();
process.OutputDataReceived += (sender, args) => stdOutput.AppendLine(args.Data); // Use AppendLine rather than Append since args.Data is one line of output, not including the newline character.
string stdError = null;
try
{
process.Start();
process.BeginOutputReadLine();
stdError = process.StandardError.ReadToEnd();
process.WaitForExit();
}
catch (Exception e)
{
throw new Exception("OS error while executing " + Format(filename, arguments)+ ": " + e.Message, e);
}
if (process.ExitCode == 0)
{
return stdOutput.ToString();
}
else
{
var message = new StringBuilder();
if (!string.IsNullOrEmpty(stdError))
{
message.AppendLine(stdError);
}
if (stdOutput.Length != 0)
{
message.AppendLine("Std output:");
message.AppendLine(stdOutput.ToString());
}
throw new Exception(Format(filename, arguments) + " finished with exit code = " + process.ExitCode + ": " + message);
}
}
private string Format(string filename, string arguments)
{
return "'" + filename +
((string.IsNullOrEmpty(arguments)) ? string.Empty : " " + arguments) +
"'";
}
The accepted answer on this page has a weakness that is troublesome in rare situations. There are two file handles which programs write to by convention, stdout, and stderr.
If you just read a single file handle such as the answer from Ray, and the program you are starting writes enough output to stderr, it will fill up the output stderr buffer and block. Then your two processes are deadlocked. The buffer size may be 4K.
This is extremely rare on short-lived programs, but if you have a long running program which repeatedly outputs to stderr, it will happen eventually. This is tricky to debug and track down.
There are a couple good ways to deal with this.
One way is to execute cmd.exe instead of your program and use the /c argument to cmd.exe to invoke your program along with the "2>&1" argument to cmd.exe to tell it to merge stdout and stderr.
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c mycmd.exe 2>&1";
Another way is to use a programming model which reads both handles at the same time.
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = #"/c dir \windows";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = false;
p.OutputDataReceived += (a, b) => Console.WriteLine(b.Data);
p.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit();
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"program_to_call.exe");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi); ////
System.IO.StreamReader myOutput = proc.StandardOutput;
proc.WaitForExit(2000);
if (proc.HasExited)
{
string output = myOutput.ReadToEnd();
}
You will need to use ProcessStartInfo with RedirectStandardOutput enabled - then you can read the output stream. You might find it easier to use ">" to redirect the output to a file (via the OS), and then simply read the file.
[edit: like what Ray did: +1]
One-liner run command:
new Process() { StartInfo = new ProcessStartInfo("echo", "Hello, World") }.Start();
Read output of command in shortest amount of reable code:
var cliProcess = new Process() {
StartInfo = new ProcessStartInfo("echo", "Hello, World") {
UseShellExecute = false,
RedirectStandardOutput = true
}
};
cliProcess.Start();
string cliOut = cliProcess.StandardOutput.ReadToEnd();
cliProcess.WaitForExit();
cliProcess.Close();
In case you also need to execute some command in the cmd.exe, you can do the following:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C vol";
p.Start();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
This returns just the output of the command itself:
You can also use StandardInput instead of StartInfo.Arguments:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.Start();
// Read the output stream first and then wait.
p.StandardInput.WriteLine("vol");
p.StandardInput.WriteLine("exit");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
The result looks like this:
Since the most answers here dont implement the using statemant for IDisposable and some other stuff wich I think could be nessecary I will add this answer.
For C# 8.0
// Start a process with the filename or path with filename e.g. "cmd". Please note the
//using statemant
using myProcess.StartInfo.FileName = "cmd";
// add the arguments - Note add "/c" if you want to carry out tge argument in cmd and
// terminate
myProcess.StartInfo.Arguments = "/c dir";
// Allows to raise events
myProcess.EnableRaisingEvents = true;
//hosted by the application itself to not open a black cmd window
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
// Eventhander for data
myProcess.Exited += OnOutputDataRecived;
// Eventhandler for error
myProcess.ErrorDataReceived += OnErrorDataReceived;
// Eventhandler wich fires when exited
myProcess.Exited += OnExited;
// Starts the process
myProcess.Start();
//read the output before you wait for exit
myProcess.BeginOutputReadLine();
// wait for the finish - this will block (leave this out if you dont want to wait for
// it, so it runs without blocking)
process.WaitForExit();
// Handle the dataevent
private void OnOutputDataRecived(object sender, DataReceivedEventArgs e)
{
//do something with your data
Trace.WriteLine(e.Data);
}
//Handle the error
private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Trace.WriteLine(e.Data);
//do something with your exception
throw new Exception();
}
// Handle Exited event and display process information.
private void OnExited(object sender, System.EventArgs e)
{
Trace.WriteLine("Process exited");
}
Here is small example:
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
var p = Process.Start(
new ProcessStartInfo("git", "branch --show-current")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
WorkingDirectory = Environment.CurrentDirectory
}
);
p.WaitForExit();
string branchName =p.StandardOutput.ReadToEnd().TrimEnd();
string errorInfoIfAny =p.StandardError.ReadToEnd().TrimEnd();
if (errorInfoIfAny.Length != 0)
{
Console.WriteLine($"error: {errorInfoIfAny}");
}
else {
Console.WriteLine($"branch: {branchName}");
}
}
}
I believe this is shortest form.
Please notice that most of command line tools easily confuse standard output and standard error, sometimes it makes sense just to clue those together into single string.
Also p.ExitCode might be sometimes useful.
Example above serves for purpose of writing command line utility like tools if you want to do it by yourself. Please note that for cli automation it's also possible to use Cake Frosten and Cake Git extension.
You can launch any command line program using the Process class, and set the StandardOutput property of the Process instance with a stream reader you create (either based on a string or a memory location). After the process completes, you can then do whatever diff you need to on that stream.
This might be useful for someone if your attempting to query the local ARP cache on a PC/Server.
List<string[]> results = new List<string[]>();
using (Process p = new Process())
{
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/c arp -a";
p.StartInfo.FileName = #"C:\Windows\System32\cmd.exe";
p.Start();
string line;
while ((line = p.StandardOutput.ReadLine()) != null)
{
if (line != "" && !line.Contains("Interface") && !line.Contains("Physical Address"))
{
var lineArr = line.Trim().Split(' ').Select(n => n).Where(n => !string.IsNullOrEmpty(n)).ToArray();
var arrResult = new string[]
{
lineArr[0],
lineArr[1],
lineArr[2]
};
results.Add(arrResult);
}
}
p.WaitForExit();
}
This may not be the best/easiest way, but may be an option:
When you execute from your code, add " > output.txt" and then read in the output.txt file.
There is a ProcessHelper Class in PublicDomain open source code which might interest you.
Julian's solution is tested working with some minor corrections. The following is an example that also used https://sourceforge.net/projects/bat-to-exe/ GenericConsole.cs and https://www.codeproject.com/Articles/19225/Bat-file-compiler program.txt for args part:
using System;
using System.Text; //StringBuilder
using System.Diagnostics;
using System.IO;
class Program
{
private static bool redirectStandardOutput = true;
private static string buildargument(string[] args)
{
StringBuilder arg = new StringBuilder();
for (int i = 0; i < args.Length; i++)
{
arg.Append("\"" + args[i] + "\" ");
}
return arg.ToString();
}
static void Main(string[] args)
{
Process prc = new Process();
prc.StartInfo = //new ProcessStartInfo("cmd.exe", String.Format("/c \"\"{0}\" {1}", Path.Combine(Environment.CurrentDirectory, "mapTargetIDToTargetNameA3.bat"), buildargument(args)));
//new ProcessStartInfo(Path.Combine(Environment.CurrentDirectory, "mapTargetIDToTargetNameA3.bat"), buildargument(args));
new ProcessStartInfo("mapTargetIDToTargetNameA3.bat");
prc.StartInfo.Arguments = buildargument(args);
prc.EnableRaisingEvents = true;
if (redirectStandardOutput == true)
{
prc.StartInfo.UseShellExecute = false;
}
else
{
prc.StartInfo.UseShellExecute = true;
}
prc.StartInfo.CreateNoWindow = true;
prc.OutputDataReceived += OnOutputDataRecived;
prc.ErrorDataReceived += OnErrorDataReceived;
//prc.Exited += OnExited;
prc.StartInfo.RedirectStandardOutput = redirectStandardOutput;
prc.StartInfo.RedirectStandardError = redirectStandardOutput;
try
{
prc.Start();
prc.BeginOutputReadLine();
prc.BeginErrorReadLine();
prc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine("OS error: " + e.Message);
}
prc.Close();
}
// Handle the dataevent
private static void OnOutputDataRecived(object sender, DataReceivedEventArgs e)
{
//do something with your data
Console.WriteLine(e.Data);
}
//Handle the error
private static void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
// Handle Exited event and display process information.
//private static void OnExited(object sender, System.EventArgs e)
//{
// var process = sender as Process;
// if (process != null)
// {
// Console.WriteLine("ExitCode: " + process.ExitCode);
// }
// else
// {
// Console.WriteLine("Process exited");
// }
//}
}
The code need to compile inside VS2007, using commandline csc.exe generated executable will not show console output correctly, or even crash with CLR20r3 error. Comment out the OnExited event process, the console output of the bat to exe will be more like the original bat console output.
Just for fun, here's my completed solution for getting PYTHON output - under a button click - with error reporting. Just add a button called "butPython" and a label called "llHello"...
private void butPython(object sender, EventArgs e)
{
llHello.Text = "Calling Python...";
this.Refresh();
Tuple<String,String> python = GoPython(#"C:\Users\BLAH\Desktop\Code\Python\BLAH.py");
llHello.Text = python.Item1; // Show result.
if (python.Item2.Length > 0) MessageBox.Show("Sorry, there was an error:" + Environment.NewLine + python.Item2);
}
public Tuple<String,String> GoPython(string pythonFile, string moreArgs = "")
{
ProcessStartInfo PSI = new ProcessStartInfo();
PSI.FileName = "py.exe";
PSI.Arguments = string.Format("\"{0}\" {1}", pythonFile, moreArgs);
PSI.CreateNoWindow = true;
PSI.UseShellExecute = false;
PSI.RedirectStandardError = true;
PSI.RedirectStandardOutput = true;
using (Process process = Process.Start(PSI))
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd(); // Error(s)!!
string result = reader.ReadToEnd(); // What we want.
return new Tuple<String,String> (result,stderr);
}
}