Reading output from console through asp.net - c#

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.

Related

How to start a process in the background and read standard output

I am new to C#, I have to start a very time consuming process from my C# program of course without bearing the loss of ui freeze, also I want to read the output printed by the program in cmd and at last I want a stop button so that I can close the program whenever I want...
Please help..
try:
using System.Diagnostics;
void startProcess()
{
Process p = new Process();
p.StartInfo.FileName = "FileName";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
var output = p.StandardOutput.ReadToEnd();
}
MethodInvoker starter = new MethodInvoker(startProcess);
starter.BeginInvoke(null, null);
for ending the process:
p.close()
Use something like this:
void StartProcess(){
Process p = new Process();
p.StartInfo.FileName = "yourfile.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
var readingThread = new System.Threading.Thread(() => {
while (!p.StandardOutput.EndOfStream){
Console.WriteLine(p.StandartOutput.ReadLine());
System.Threading.Thread.Sleep(1);
}
}
readingThread.Start();
}

output limitation when calling exe file from c# code

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();
}

WPF C# - get python version by cmd

I've try to taken python version, so i've start a process with cmd and a command "python --version".
I've try this for first:
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/k C:/Python36/python --version";
p.StartInfo.CreateNoWindow = false;
var retorno = p.Start();
}
and opened a cmd window and returned this:
cmd return
instead of this, i need this result returned to my WPF application, so i try this:
public static string GetPythonVersion()
{
string command = "python --version";
string output = null;
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = String.Format(#"/c {0}\{1} ", "C:/Python36/", command);
p.StartInfo.CreateNoWindow = true;
if (p.Start())
output = p.StandardOutput.ReadToEnd();
}
return output;
}
return empty string to me.
however to an example if i using the same code to return "pip list" to my wpf application working well, but in this case to taken the version the string return empty....
Ok, guys.I've discovered what is wrong on the code. Below the correct code:
public static string GetPythonVersion()
{
string command = "python --version";
string output = null;
using (System.Diagnostics.Process p = new System.Diagnostics.Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = String.Format(#"/c {0}\{1} ", "C:/Python36/", command);
p.StartInfo.CreateNoWindow = true;
if (p.Start())
output = p.StandardError.ReadToEnd();
}
return output;
}
yeah, the python version is on StandardError, because, as you can see on :
https://docs.python.org/3/using/cmdline.html#generic-options
"Print the Python version number and exit."
So when run the command on cmd, at the StandardOutput there is nothin...
thanks to everyone for trying to help me ! now this case is over!

Reading output from console through asp.net

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();

C sharp calls Java using cmd

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;
}

Categories