完成后保持控制台窗口打开

时间:2012-10-17 00:21:29

标签: batch-file console

我正在运行一个bat文件,并在控制台窗口中显示输出。现在,我正在故意失败bat文件(bat文件包含“STARET www.webaddress.com”)。错误捕获并且一切正常,但控制台窗口立即关闭。我想保持开放,但到目前为止,我的搜索方法并未提出任何解决方案。

我目前的代码:

Me.processStartInfo = New ProcessStartInfo("C:\Admin\PsTools\psexec.exe", "\\PCName -u domain\" & user & " -p " & pass & " pathtobatfile")
Me.processStartInfo.RedirectStandardError = True
Me.processStartInfo.RedirectStandardOutput = True
Me.processStartInfo.WindowStyle = ProcessWindowStyle.Hidden
Me.processStartInfo.UseShellExecute = False
Dim process As System.Diagnostics.Process = Nothing

'Start the process, which runs the bat file on the remote server
process = System.Diagnostics.Process.Start(Me.processStartInfo)
AddHandler process.OutputDataReceived, AddressOf OutputHandler
process.BeginOutputReadLine()
process.WaitForExit()

Dim errorresponse As DialogResult
   If process.HasExited And process.ExitCode <> 0 Then
      errorresponse = MessageBox.Show("There was an error. Exit code: " & process.ExitCode & _
                        ". Do you wish to view the log?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
        ElseIf process.HasExited And process.ExitCode = 0 Then
            MessageBox.Show("The update completed successfully.", "Success", MessageBoxButtons.OK)
        End If
        If errorresponse = DialogResult.Yes Then
          'To fill in later
        End If

事件处理程序子:

Private Shared Sub OutputHandler(ByVal sendingProcess As Object, ByVal output As DataReceivedEventArgs)
    Console.WriteLine(output.Data)
End Sub

我也尝试过做同步输出(使用StreamReader和带有Readline()的Console.Writeline),但这也不起作用。

2 个答案:

答案 0 :(得分:1)

尝试在最后添加

set err=%ERRORLEVEL%
pause
exit %err%

答案 1 :(得分:0)

尝试在pause文件的末尾添加BAT。或者,如果您通过cmd进程发布bat文件,则可以添加/k参数。

cmd /k pathToBatFile