我尝试在文本框中执行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
答案 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()