2014-08-30 2 views
0

이 코드를 사용하여 두 개의 숫자를 asp.net을 통해 C 프로그램 파일의 .exe에 입력으로 전달한 다음 콘솔에서 출력을 읽으려고합니다. 콘솔에서 출력물을 읽는 데 문제가 있습니다.asp.net을 통해 콘솔에서 출력 읽기

내 asp.net 코드입니다.

문자열 반환 값;

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

입력을 전달하는 코드입니다.

#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

그리고 ... 무엇이 문제입니까? –

+0

콘솔에서 출력을 읽을 수 없습니다. –

답변

0

콘솔 창이 숨겨져 있고 SendKeys가 활성 창에 쓰고 자식 프로세스 windw가 숨겨져 있기 때문에 SendKeys가 작동하는지 잘 모르겠지만 StandardInput.WriteLine을 사용하여 자식에게 데이터를 보내면 프로세스가 작동해야합니다.

이 코드는 작동하고 다음과 같은 내용으로 파일 AdderOutput.txt를 만듭니다 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(); 
     } 
    } 
} 

그것은되지 않을 수도 =

입력 숫자
합계를 추가하는 두 개의 번호를 입력 가장 좋은 해결책은 - C#을한지 꽤 오래되었지만 작동하는 것 같습니다. adder.exe은 코드의 C 프로그램입니다.

+0

입력을 전달하지 않습니다. 콘솔 창이 숨겨져 있지만 .exe 파일 만 열립니다. –

+0

@AdilShafiq 나는 당신을 이해하는지 모르겠다. 내 프로그램이 작동하지 않거나 다른 문제가 있다는 말을하고 있습니까? – jpw

+0

쓰기 라인을 사용하는 대신 입력을 전달하지 않으면 콘솔에서 exe를 열고 키보드로 입력을 요청합니다. –