I am using this code to pass two numbers as input to an .exe of a C program file through asp.net and after that trying to read the output from console. I am having problem to read any output from the console.
My asp.net code is.
string returnvalue;
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.FileName = ("C:\\Users\\...\\noname01.exe");
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
Thread.Sleep(500);
SendKeys.SendWait("1");
Thread.Sleep(500);
SendKeys.SendWait("~");
Thread.Sleep(500);
SendKeys.SendWait("2");
Thread.Sleep(500);
SendKeys.SendWait("~");
Thread.Sleep(500);
StreamReader sr = p.StandardOutput;
returnvalue = sr.ReadToEnd();
System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\...\\StudentOutput.txt");
file.WriteLine(returnvalue);
My C code to which am passing inputs is.
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}
I am having problem to read any output from the console.
use StandardInput, rather than in the SendKeys.
StreamWriter sw = p.StandardInput;
sw.WriteLine("1");
sw.WriteLine("2");
sw.Close();
...
p.WaitForExit();
p.Close();
System.IO.StreamWriter file = new System.IO.StreamWriter(".\\out.txt");//Simplified
file.WriteLine(returnvalue);
file.Close();
Related
I have an exe file that I call it from my c# code by using Process namespace in c#.
I catch output by using ReadToEnd() method. (in exe source code i used a print method for my desired output).
the problem is when my output is getting larger the executing of exe file is got crashed and hanged.
Is there any limitation of output size when calling exe function?
Process p = new Process();
p.StartInfo.FileName = command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Arguments = args;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
return output;
What you're experiencing is a deadlock. That situation is described in the documentation. In order to avoid it, make sure you read the output before waiting for the child process to exit.
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
I remember having the same problem as you're describing here, and what I did back then was something like this:
private string RunProcess(string command, string args)
{
StringBuilder sb = new StringBuilder();
Process p = new Process();
p.StartInfo.FileName = command;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Arguments = args;
p.OutputDataReceived += (sender, eventArgs) => { sb.Append(eventArgs.Data); };
p.Start();
p.WaitForExit();
return sb.ToString();
}
Summary:
I have a WinForm application doing some works and writing some output with System.Console, Here is the code:
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Console.WriteLine("Some String");
return 1;
}
Suppose we name it SubApp.exe
In another program, I want to execute this subApp.exe and read the output which it creates with System.Diagnostics.Process. Here is my code:
System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
int exitCode = p.ExitCode;
Question:
But unfortunately, both output and error is empty and even worse the exitCode is 0. What is the problem? Am I doing the procedure in the right way?
Make your SubApp to write argument to the console then it will return the console output to the process output
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Console.WriteLine(args[0]); // this will print only 'Some' part of argument all string will be in args seperating by space
Console.WriteLine(args[1]); // this will print "Parameter"
return 1;
}
then call your SubApp
System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
int exitCode = p.ExitCode;
after executing this you will get Some Parameter into your output variable
I am using this code to pass two numbers as input to an .exe of a C program file through asp.net and after that trying to read the output from console. I am having problem to read any output from the console.
My asp.net code is.
string returnvalue;
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.FileName = ("C:\\Users\\...\\noname01.exe");
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
Thread.Sleep(500);
SendKeys.SendWait("1");
Thread.Sleep(500);
SendKeys.SendWait("~");
Thread.Sleep(500);
SendKeys.SendWait("2");
Thread.Sleep(500);
SendKeys.SendWait("~");
Thread.Sleep(500);
StreamReader sr = p.StandardOutput;
returnvalue = sr.ReadToEnd();
System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Hussain\\Documents\\Visual Studio 2012\\WebSites\\WebSite4\\Data\\StudentOutput.txt");
file.WriteLine(returnvalue);
My C code to which am passing inputs is.
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}
Any kind of help required.
I'm not sure if SendKeys works in this case as the console window is hidden and SendKeys is supposed to write to the active window and the child process windw is hidden, but if you use StandardInput.WriteLine to send data to the child process it should work.
This code works and creates a file AdderOutput.txt with the following content:
Enter two numbers to add
Sum of entered numbers = 3
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string returnvalue;
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.FileName = ("D:\\adder.exe");
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
Thread.Sleep(500);
p.StandardInput.WriteLine("1");
Thread.Sleep(500);
p.StandardInput.WriteLine("2");
Thread.Sleep(500);
StreamReader sr = p.StandardOutput;
returnvalue = sr.ReadToEnd();
System.IO.StreamWriter file = new System.IO.StreamWriter("D:\\AdderOutput.txt");
file.WriteLine(returnvalue);
file.Flush();
file.Close();
}
}
}
It might not be the best solution - it's been a while since I did C# - but it seems to work. The adder.exeused is the C program from your code.
I want to get the output of an execution in c# and I referred to this question. But I only have the output be printed on the console but not stored in the specified string. Here is my code: `
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = " -i 1.flv";
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
Console.WriteLine(output);
Console.ReadLine();`
The output string is still empty after the execution of these codes. Plus if I keep the line p.StartInfo.CreateNoWindow = true;, no words will be printed on the console at all, why this happen? I thought the line will only stop a new window being created.
Move string output = p.StandardOutput.ReadToEnd(); inside wait for exit.
How are you going to read data when it already exit.
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = " -i 1.flv";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();`
I'd try the following:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = " -i 1.flv";
p.Start();
while (!p.HasExited)
{
string output = p.StandardOutput.ReadToEnd();
}
I also suggest you have a look at the BeginReadOutputLine method in this example given in the MS documentation. Being asynchronous, it will be called even though you use WaitForExit.
A boiled down version of this is:
// Start the asynchronous read of the output stream.
p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
p.EnableRaisingEvents = true;
p.BeginOutputReadLine();
p.Start();
p.WaitForExit();
p.Close();
private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
// Collect the command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
numOutputLines++;
// Add the text to the output
Console.WriteLine(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);
}
}
How about switch those two line?
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
I am trying to write a c sharp program to call a jar file.
I've searched for a lot of solutions about this type of question, but I just can't figure it out.
Here is my situation, before really calling my jar file, I try to call java -version for a test.
This is a method that executes command line with the arguments cmd
public static string StartCmdProcess(string[] cmd)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i++)
{
p.StandardInput.WriteLine(cmd[i].ToString());
}
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return strRst;
}
and in my main method
I call
string result = StartCmdProcess(new string[] { "java -version" });
Console.WriteLine(result);
When I call "java", it works fine and outputs everything that's supposed to be printed.
But, when I put my arguments like java -version, it just doesn't work.
Is there anyone knows what is wrong with this problem?
Greatly appreciated if anyone could help :)
EDITED: The result of calling actually goes to StandardError rather than StandardOutput, but that's pretty wired. Anyone knows why?
public static string StartCmdProcess(string[] cmd)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i++)
{
p.StandardInput.WriteLine(cmd[i].ToString());
}
p.StandardInput.WriteLine("exit");
strRst.Append(p.StandardOutput.ReadToEnd()).Append(p.StandardError.ReadToEnd());
p.WaitForExit();
p.Close();
return strRst.ToString();
}
You need to set your arguments seperate from your command.
p.StartInfo.FileName = "java";
p.StartInfo.Arguments = "-version";
When I tested this the output was redirected but it came from the standard error rather than the standard output. Unsure as to why but you could return both streams like so:
StringBuilder strRst = new StringBuilder();
strRst.AppendLine(p.StandardOutput.ReadToEnd());
strRst.AppendLine(p.StandardError.ReadToEnd());
p.WaitForExit();
p.Close();
return strRst.ToString();
You need to modify your method to make use of the cmd parameter as follows
public static string StartCmdProcess(string[] cmd)
{
System.Diagnostics.Process p = new System.Diagnostics.Process
{
StartInfo =
{
FileName = cmd[0],
Arguments = cmd.Length>1?cmd[1]:"",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
}
};
p.Start();
p.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i++)
{
p.StandardInput.WriteLine(cmd[i].ToString());
}
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return strRst;
}