我正在尝试在我的 vb.net 项目中读取/使用 python 程序的输出,到目前为止我没有得到任何结果。我想看到的是 python 程序运行(首先自己运行)并且所有输出都被重定向到一个文本框。
我看过其他一些关于这个的帖子,但我要么遗漏了一些东西,要么不理解一些东西,因为我得到的只是空白输出。公开课形式1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim PythonPath = "C:\Python27\"
Dim strPath As String = Application.StartupPath
MessageBox.Show(PythonPath & "python.exe """ & strPath & "\Resources\import_logs.py"" ")
Dim start_info As New ProcessStartInfo(TextBox1.Text)
' Make the process and set its start information.
Dim process As New Process()
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.StartInfo.FileName = PythonPath & "\python.exe"
process.StartInfo.Arguments = """" & strPath & "\resources\import_logs.py"""""
process.StartInfo.UseShellExecute = False
process.StartInfo.CreateNoWindow = True
process.StartInfo.RedirectStandardOutput = True
'process.StartInfo.RedirectStandardError = True
AddHandler process.OutputDataReceived, AddressOf proccess_OutputDataReceived
process.Start()
process.BeginOutputReadLine()
End Sub
Public Sub proccess_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
On Error Resume Next
' output will be in string e.Data
' modify TextBox.Text here
'Server_Logs.Text = e.Data ` Does not display anything in textbox
MsgBox(e.Data) 'It works but I want output in text box field
End Sub
End Class
最终,我要将参数传递给 python 脚本,并且我想获得我可以使用的反馈(将错误插入数据库,完成后发送电子邮件等),所以我希望它能够捕获过程在运行时,而不仅仅是最后的数据转储。
任何帮助将非常感激。