简体   繁体   中英

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:\\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.exe used is the C program from your code.

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