简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM