VB.net - 如果在bat执行完成后如何添加?

时间:2017-01-31 12:10:17

标签: vb.net batch-file

我尝试在文本框中执行bat文件。我成功了,现在我想在执行完成后添加一个if以查看它是否成功。怎么做?

我的代码:

        Using P As Process = New Process
        P.StartInfo.CreateNoWindow = True
        P.StartInfo.UseShellExecute = False
        P.StartInfo.WorkingDirectory = "E:\Desktop"
        P.StartInfo.RedirectStandardInput = True
        P.StartInfo.RedirectStandardOutput = True
        P.StartInfo.RedirectStandardError = True
        P.StartInfo.FileName = "E:\Desktop\test.bat"
        P.Start()
        Dim sOutput As String
        Using oStreamReader As System.IO.StreamReader = P.StandardOutput
            sOutput = oStreamReader.ReadToEnd()
        End Using
        TextBox1.Text = sOutput
        P.Close()
    End Using

如果:

If System.IO.File.Exists("C:\MyFile.txt") = False Then
            MsgBox("Field does not exist!", MsgBoxStyle.Critical, "File Not Found")
        Else
            MsgBox("Field exists!", MsgBoxStyle.Critical, "File Found")
        End If
    End If

1 个答案:

答案 0 :(得分:0)

您可以同步等待该过程完成:

p.WaitForExit(10000)  ' wait at most 10 000 ms

或者您可以异步订阅流程' Exited event

p.EnableRaisingEvents = True

AddHandler p.Exited, 
    Sub(sender, e)
        MsgBox("Process finished")
    End Sub

p.Start()