2014-11-04 211 views
0

我想開發一個c#應用程序(a.exe)輸入到另一個c#應用程序(b.exe) b.exe不接受任何參數。從控制檯應用程序輸入到另一個C#控制檯應用程序?

b.exe -call or b.exe call等等不起作用

當我打開b.exe從CMD它需要的參數是這樣的:

b:call 
b:command 2 

.... 

b:command n 

我怎麼能養活b.exea.exe

我已經使用,但它不起作用。

 var psi = new ProcessStartInfo("b.exe") 
     { 
      RedirectStandardInput = true, 
      RedirectStandardOutput = false, 
      UseShellExecute = false 
     }; 

     var p = new Process { StartInfo = psi }; 

     p.Start(); 
     p.StandardInput.WriteLine("call"); 
+0

你需要應用程序或基本shell重定向嗎?即'a.exe | b.exe'? – 2014-11-04 16:39:29

+0

我想實現一個應用程序可以調用b.exe的命令 – david 2014-11-04 16:43:31

+0

b:請致電 b:help b:com1 – david 2014-11-04 16:43:59

回答

-1

以下是我從你的問題的理解:

  • a.exe開始b.exe
  • a.exe使用任何命令行參數或標準輸入的命令/請求傳遞到b.exe
  • b.exe收到命令/請求並通過StandardOutput傳遞信息

使用命令行參數更簡單一點,除非您需要爲每個請求/命令多次運行b.exe

// b.exe 
static void Main(string[] args) 
{ 
    // using command line arguments 
    foreach (var arg in args) 
     ProcessArg(arg); 

    // if using input, then use something like if processing line by line 
    // string line = Console.ReadLine(); 
    // while (line != "exit" || !string.IsNullOrEmpty(line)) 
    //  ProcessArg(line); 

    // or you can read the input stream 
    var reader = new StreamReader(Console.OpenStandardInput()); 
    var text = reader.ReadToEnd(); 
    // TODO: parse the text and find the commands 
} 

static void ProcessArg(string arg) 
{ 
    if (arg == "help") Console.WriteLine("b.exe help output"); 
    if (arg == "call") Console.WriteLine("b.exe call output"); 
    if (arg == "command") Console.WriteLine("b.exe command output"); 
} 

// a.exe 
static void Main(string[] args) 
{ 
    // Testing -- Send help, call, command --> to b.exe 
    var psi = new ProcessStartInfo("b.exe", "help call command") 
    { 
     RedirectStandardError = true, 
     RedirectStandardInput = true, 
     RedirectStandardOutput = true, 
     UseShellExecute = false, 
    }; 
    var p = new Process() { StartInfo = psi }; 
    p.ErrorDataReceived += p_ErrorDataReceived; 
    p.OutputDataReceived += p_OutputDataReceived; 
    p.Start(); 
    p.BeginErrorReadLine(); 
    p.BeginOutputReadLine(); 

    // if sending data through standard input (May need to use Write instead 
    // of WriteLine. May also need to send end of stream (0x04 or ^D/Ctrl-D) 
    StreamWriter writer = p.StandardInput; 
    writer.WriteLine("help"); 
    writer.WriteLine("call"); 
    writer.WriteLine("exit"); 
    writer.Close(); 

    p.WaitForExit(); 
} 

static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    Console.Error.WriteLine("a.exe error: " + e.Data ?? "(null)"); 
} 

static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    Console.WriteLine("a.exe received: " + e.Data ?? "(null)"); 
} 

看那OutputDataReceivedErrorDataReceived事件以及這些方法的MSDN樣本。

其中一個考慮因素是您無法通過管理權限管理運行程序的輸出。您需要保存輸出,然後將輸出傳遞給其他程序。

我發現了一個有幫助的CSharpTest庫,其中有一個ProcessRunner類。例如,瀏覽ProcessRunner的「InternalStart」方法。

+0

如果'a.exe'需要從'b.exe'讀取輸出,則需要OutputDataReceived事件。這很令人困惑,因爲它似乎是輸入,但它是'b.exe'過程的輸出。 – Ryan 2014-11-04 17:54:52

+0

我無法訪問b.exe的源代碼, 我想運行一個命令,如b:來自a.exe的調用, 我已啓用RedirectiStandardInput,但沒有來自b.exe的響應 – david 2014-11-05 07:40:15

+0

命令之間的分隔符是什麼? NewLine,空間還是其他?我只是製作了'a.exe'和'b.exe',它工作。你是否從命令行測試過'b.exe'?你能輸入命令嗎?它會迴應嗎?你有'exit'命令還是查找流的結尾?這些是你需要找到答案的問題。 – Ryan 2014-11-06 06:18:25

相關問題