这是我用来使用 ProcessStartInfo 运行 RoboCopy 的一些未经编辑的实用程序代码。它捕获输出流(在这种情况下,代码不会对它做任何事情。)
我使用 Kellerman SFTP 库——效果很好,而且不贵。
Sub RunRobo(ByVal SD As String, ByVal DD As String, ByVal Job As String, ByRef Out As String, ByRef Err As String)
' runs RoboCopy and returns log text
' typical job: "*.* /V /NDL /S /E /COPY:DAT /PURGE /NP /R:0 /W:0 "
Try
Application.DoEvents()
Dim sParams As String = SD & " " & DD & " " & Job
'If Not ShowHeadFoot Then sParams &= " /NJH /NJS"
' Set start information.
Dim start_info As New ProcessStartInfo("RoboCopy", sParams)
start_info.UseShellExecute = False
start_info.CreateNoWindow = True
start_info.RedirectStandardOutput = True
start_info.RedirectStandardError = True
' Make the process and set its start information.
Dim proc As New Process()
proc.StartInfo = start_info
' Start the process.
proc.Start()
' Attach to stdout and stderr.
Dim std_out As System.IO.StreamReader = proc.StandardOutput()
Dim std_err As System.IO.StreamReader = proc.StandardError()
' Display the results.
Out = std_out.ReadToEnd()
Err = std_err.ReadToEnd()
'Dim aLines() As String = sOut.Split(vbCrLf) ' if no header/footer creates array of file info
' more generic would be to return via params
'txtStreetsErr.Text = sErr
'txtStreetsOut.Text = sOut
Try
std_out.Close()
std_err.Close()
Catch ex As Exception
End Try
Try
proc.Close()
Catch ex As Exception
End Try
Catch ex As Exception
MsgBox(ex.Message, , "Error during RoboCopy")
Finally
'Cursor = Cursors.Default
End Try
End Sub