【问题标题】:Reading output from console through asp.net通过 asp.net 从控制台读取输出
【发布时间】:2014-08-31 10:04:24
【问题描述】:

我正在使用此代码通过 asp.net 将两个数字作为输入传递给 C 程序文件的 .exe,然后尝试从控制台读取输出。我在从控制台读取任何输出时遇到问题。 我的asp.net代码是。

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

传递输入的我的 C 代码是。

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

我无法从控制台读取任何输出。

【问题讨论】:

标签: c# c++ asp.net c


【解决方案1】:

使用StandardInput,而不是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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2023-03-21
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    相关资源
    最近更新 更多