0
我運行下面的代碼來從vb.net應用執行命令。CMD過程不離開
Dim CMDServer As Diagnostics.ProcessStartInfo
Dim CMDReply As Diagnostics.Process
CMDServer = New Diagnostics.ProcessStartInfo
CMDServer.WorkingDirectory = "C:/"
CMDServer.FileName = "cmd.exe"
CMDServer.UseShellExecute = False
CMDServer.RedirectStandardOutput = True
CMDServer.RedirectStandardError = True
CMDServer.CreateNoWindow = True
CMDServer.Arguments = "/C " + command
CMDReply = Process.Start(CMDServer)
MsgBox("START")
Dim Reply As String = ""
If Not CMDReply.HasExited Then
CMDReply.WaitForExit()
End If
MsgBox("EXIT")
Try
Dim ext = CMDReply.ExitCode
Reply = CMDReply.StandardOutput.ReadToEnd()
MsgBox(ext.ToString + " " + Reply)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
它工作得很好,幾乎所有的命令,但是當command="help"
程序stucks上CMDReply.WaitForExit()
。任何人都可以解釋什麼可能是這裏的問題?
標準僵局。先讀取輸出並*然後*等待退出。程序無法退出,直到您讀取其所有*的輸出。只有只有很少的輸出符合緩衝區時,您的代碼才能工作。 –
@HansPassant好的,謝謝,它的工作.. – rahul
@HansPassant:請張貼作爲答案被接受。 – Neolisk