5
A
回答
4
本示例假定Windows窗體應用程序帶有兩個文本框(RunResults
和Errors
)。
// Remember to also add a using System.Diagnostics at the top of the class
private void RunIt_Click(object sender, EventArgs e)
{
using (Process p = new Process())
{
p.StartInfo.WorkingDirectory = "<path to batch file folder>";
p.StartInfo.FileName = "<path to batch file itself>";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.WaitForExit();
// Capture output from batch file written to stdout and put in the
// RunResults textbox
string output = p.StandardOutput.ReadToEnd();
if (!String.IsNullOrEmpty(output) && output.Trim() != "")
{
this.RunResults.Text = output;
}
// Capture any errors written to stderr and put in the errors textbox.
string errors = p.StandardError.ReadToEnd();
if (!String.IsNullOrEmpty(errors) & errors.Trim() != ""))
{
this.Errors.Text = errors;
}
}
}
更新時間:
樣品上方是一個名爲RunIt
按鈕一個按鈕單擊事件。表單上有幾個文本框,RunResults
和Errors
,其中我們將stdout
和stderr
的結果寫入。
5
System.Diagnotics.Process.Start(「yourbatch.bat」);應該這樣做。
1
我推斷出在GUI窗體中執行你的意思是在一些UI控件中顯示執行結果。
也許是這樣的:
private void runSyncAndGetResults_Click(object sender, System.EventArgs e)
{
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(@"C:\batch.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process batchProcess;
batchProcess = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = batchProcess.StandardOutput;
batchProcess.WaitForExit(2000);
if (batchProcess.HasExited)
{
string output = myOutput.ReadToEnd();
// Print 'output' string to UI-control
}
}
來自實例here拍攝。
+0
你將如何從一個按鈕調用這個函數? – Mike 2011-05-14 22:31:09
相關問題
- 1. 以C#形式運行.Bat
- 2. 從C#運行Matlab GUI#
- 3. 運行EXE文件形式CMD與C#
- 4. 如何以Simulink塊的形式運行C++程序?
- 5. netbeans C++ googletest從gui運行測試
- 6. 如何在C#程序中運行「無頭」(無GUI)Unity3D遊戲?
- 7. 如何從GUI Jbutton運行主類?
- 8. 如何運行沒有GUI臂地圖?
- 9. 如何從windows gui運行python腳本?
- 10. 如何通過GUI運行matlab命令?
- 11. 如何運行win7的epydoc gui?
- 12. 如何在windows7上運行'git gui'?
- 13. 運行git GUI和龜GUI並行
- 14. 運行IDLE(蟒GUI)
- 15. 運行,並從GUI
- 16. 如何在運行時使用C#運行字符串公式?
- 17. 如何在沒有GUI的情況下運行MATLAB並獲得圖形結果?
- 18. 如何在隱形模式下運行qt C++控制檯應用程序?
- 19. C#形式如何,每次運行方法的快捷鍵是按下
- 20. 如何使jQuery選擇器運行得更快或以塊的形式運行
- 21. 圖形用戶界面(GUI) - 運行時出現Java錯誤
- 22. GUI運行不正常
- 23. gui無法正常運行
- 24. 運行GUI在Eclipse的Android
- 25. 從gui運行matlab腳本
- 26. 複雜形式的Visual Studio中的C++可縮放GUI窗口
- 27. C#窗體窗體,運行兩種形式
- 28. C#以系統的形式提升當前運行進程
- 29. C#DataGridView消失在另一個線程上運行的形式
- 30. 如何運行gambit-c
@Kev嗨,謝謝,這將如何初始化,但例如在一個文本框? – Mike 2011-05-14 22:25:30
@Mike - 我已經更新了我的答案,那是什麼意思? – Kev 2011-05-14 22:35:46
@Kev,嗨,是的,這是感謝你,我做了它的書面,雖然我得到一些編譯錯誤,我認爲這可能是因爲我使用NET框架3.5/VS 2008(將嘗試升級..), – Mike 2011-05-14 23:04:59