c# - 通过asp.net从控制台读取输出

标签 c# c++ asp.net c

我使用此代码通过 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;
    }

我在读取控制台的任何输出时遇到问题。

最佳答案

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

关于c# - 通过asp.net从控制台读取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25590898/

相关文章:

c# - 在 curl 中制定 POST 请求

c++ - 我再次对指针感到困惑

c# - 如何在不区分大小写的情况下从 ressourceManager 获取对象?

c# - uwp InkCanvas 将笔画保存为 svg

c# - 如何在带有自定义 ContentTemplate 的 WPF 按钮上使用 accesskey?

c++ - 文件夹不存在则创建,存在则不做任何操作

c++ - C++17 中包含 XPM 图像时发出警告

asp.net - Web 部署项目无法映射包含虚拟的路径

asp.net - ASP.NET MVC 3 路由可能存在错误?

javascript - 从 aspx 中的代码隐藏调用 JavaScript