2014-08-31 173 views
0

我正在使用此代碼將兩個數字作爲輸入傳遞給通過asp.net的C程序文件的.exe,之後嘗試從控制檯讀取輸出。我有問題要閱讀控制檯的任何輸出。 我的asp.net代碼是。通過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; 
    } 

我有問題從控制檯讀取任何輸出。

+0

入住這http://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output – malkam 2014-08-31 10:33:35

+0

我以前嘗試過了,它不工作 – 2014-08-31 18:09:02

回答

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