我可以運行使用process.start()
的命令行進程。 我可以使用標準輸入提供輸入。之後,當進程再次要求用戶輸入時,我的程序如何知道並將輸入傳遞給該exe文件?使用c運行交互式命令行exe#
回答
有一個example here聽起來類似於你需要什麼,使用Process.StandardInput
和StreamWriter
。
Process sortProcess;
sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe";
// Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = false;
// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
sortProcess.StartInfo.RedirectStandardOutput = true;
sortOutput = new StringBuilder("");
// Set our event handler to asynchronously read the sort output.
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
// Redirect standard input as well. This stream
// is used synchronously.
sortProcess.StartInfo.RedirectStandardInput = true;
sortProcess.Start();
// Use a stream writer to synchronously write the sort input.
StreamWriter sortStreamWriter = sortProcess.StandardInput;
// Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine();
Console.WriteLine("Ready to sort up to 50 lines of text");
String inputText;
int numInputLines = 0;
do
{
Console.WriteLine("Enter a text line (or press the Enter key to stop):");
inputText = Console.ReadLine();
if (!String.IsNullOrEmpty(inputText))
{
numInputLines ++;
sortStreamWriter.WriteLine(inputText);
}
}
希望幫助
如果我理解正確,我認爲您應該可以通過使用RedirectStandardOutput和RedirectStandardInput重定向標準輸入/輸出來完成此操作。
WinSCP項目有一個C#示例,用於如何使用這種方式與它進行通信。你可以在這裏找到它:SFTP file transfers in .NET(雖然此示例僅收集輸出,而不使用它所有,但該技術應該是一樣的。)
謝謝你的答覆。我嘗試過這個。但是要求用戶輸入的過程不是預定義的。該方案是像 1.我想設計在線compliler爲C# 2.我可以使用代碼創建該程序n cretae exe文件 3.現在我想通過C#運行該exe文件 4.用戶可以編寫任何類型的在diff階段需要用戶輸入的程序。 希望你能理解整個場景 – rajshades 2010-07-22 12:21:18
@rajshades:我會認爲這會是類似的,但不是一次性發送所有命令,而是發送命令,讀取輸出,發送下一個命令等我雖然沒有測試過。作爲一個普遍的說明,如果你正在創建這個exe文件,你能不能嘗試給它一些更好的方式來與它進行通信(WCF,TCP/IP協議,命名管道等)? – 2010-07-22 12:49:48
你想查找IPC。正如羅伯特所示,.NET中的Process
類將幫助您。但專門爲你的問題(如何知道何時寫數據):你不能。不是一般的。
如果您知道所需的輸入(例如「yyy」),則可以在創建的過程的STDIN
中提供。您不必等待程序請求這些信息:它只會在需要數據時從STDIN
中讀取。
如果您需要處理程序輸出以決定要寫入STDIN
的內容,請嘗試閱讀進程STDOUT
。你可能會遇到沖洗的問題,雖然...
- 1. 在Paramiko中運行交互式命令
- 2. 用apache commons運行交互命令exec
- 3. 在C運行時與命令行程序交互(stdin/out)C
- 4. 如何交互使用php命令行?
- 5. 使用Ruby的命令行交互
- 6. C的命令行交互shell
- 7. 交互命令行模式emacs
- 8. 在CLI模式下從PHP運行交互式shell命令
- 9. 如何以交互模式運行sbt multiple命令作爲一個命令?
- 10. 使用java的交互式命令Runtime.getRunTime.exec()
- 11. PyGreSQL交互式運行SQL
- 12. 運行Build.pl非交互式
- 13. 使用命令行參數從bat文件運行exe文件
- 14. C#命令行項目 - 通過.exe運行時出錯
- 15. 使用C#中的參數執行命令行.exe。
- 16. 使用命令行參數在C#中運行exe,抑制dos窗口
- 17. 如何使用C#.net從命令行調用C++ EXE .net
- 18. 快速的NodeJS命令行交互
- 19. 我可以使用交互式命令行進行Squeak/Pharo開發嗎?
- 20. 使用C#運行unix命令
- 21. 與命令行應用程序交互的最佳方式
- 22. Docker容器中的交互式命令行應用程序
- 23. 將輸入饋送到交互式命令行應用程序
- 24. 如何使用java運行時執行交互式shell腳本?
- 25. 如何從(Ruby)腳本交互地運行mount命令?
- 26. 在Python子進程中在docker中運行交互命令
- 27. 命令行參數到exe
- 28. Java應用程序與命令行進行交互
- 29. 從windows服務運行exe,與用戶桌面交互
- 30. 包括在C#中的命令行應用程序執行EXE
你可能感興趣的[這篇文章](http://www.codeducky.org/process-handling-net)涵蓋了許多錯綜複雜的工作。 NET進程,尤其是在處理輸入和輸出 – ChaseMedallion 2014-08-29 01:38:17