gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:29:32 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com