2013-10-29 30 views
0

在我最近的任務中,我需要執行一個java命令並從中獲得一些價值,並在我的程序中使用它。從CMD提示輸出

我在網上看了很多關於它的論壇,但非在爲我工作。

我試着創建它的bat文件並在我的程序中執行它,但那也不起作用。

當我在命令提示符下執行它或直接執行bat文件時,它就起作用了。但是,當我從應用程序/ exe執行,然後失敗。

我還需要輸出。

任何幫助,高度讚賞。

在此先感謝。

+0

你能後的批處理文件,工程 – Chelseawillrecover

+0

的environement變量必須avaialble的細節當你從命令提示符運行它時,它將從代碼執行時不可用。你可以分享你從應用程序運行時看到的錯誤嗎? –

+0

「不起作用」不是錯誤描述。我們不能以這種方式幫助你。發佈詳情。 – nvoigt

回答

0

示例代碼:

// Start the child process. 
Process p = new Process(); 
// Redirect the output stream of the child process. 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "YOURBATCHFILE.bat"; 
p.Start(); 
// Do not wait for the child process to exit before 
// reading to the end of its redirected stream. 
// p.WaitForExit(); 
// Read the output stream first and then wait. 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 

的問題可以幫助你How To: Execute command line in C#, get STD OUT results