Redirect XCOPY output from the console to a rich text box control

Ok, this about has me tied in knots! I am trying to use xcopy in my program to copy files from one location to another location and have the info normally displayed in a console window redirected to a rich text box. I cannot get it to work. I don't get any errors. The code snippit is below. I can use "/K dir *.*" for the Arguments and it redirects fine. I am completely stumped.

Dim myprocess As New Process
Dim info As New ProcessStartInfo

With info
'.CreateNoWindow = True
.FileName = "cmd.exe"
.Arguments = "/K xcopy.exe C:\Test\Trash1 C:\Test\Trash2 /S /I /-Y"
'.WorkingDirectory = Application.StartupPath
.UseShellExecute = False
.RedirectStandardOutput = True
.RedirectStandardError = True
End With


myprocess.StartInfo = info
myprocess.Start()
myprocess.WaitForExit()

'Dim output As String = myprocess.StandardOutput.ReadToEnd
'rtbOutput.Text = output




Answer this question

Redirect XCOPY output from the console to a rich text box control

  • epsilon_ro

    That worked perfect. I don't understand all the code but I will keep stepping thru until I get it.

    Thanks again!


  • Vadimus

    Okay, I went back and reworked both the previous answers

    into a better one. ('real time' textbox update).

    (Example uses a textbox rather than a richtextbox, but you can

    simply change that.)

    Imports System.Threading

    Public Class Form1

    Private _SetText As SetText

    Friend WithEvents backgroundWorker1 _

    As System.ComponentModel.BackgroundWorker = _

    New System.ComponentModel.BackgroundWorker()

    Delegate Sub SetText(ByVal line As String)

    Private Sub StartXCopy_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles StartXCopy.Click

    _SetText = New SetText(AddressOf MySetText)

    Me.backgroundWorker1.WorkerSupportsCancellation = True

    Me.backgroundWorker1.RunWorkerAsync()

    End Sub

    Private Sub MySetText(ByVal line As String)

    TextBox1.Text = TextBox1.Text & line

    Application.DoEvents()

    End Sub

    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, _

    ByVal e As System.ComponentModel.DoWorkEventArgs) _

    Handles backgroundWorker1.DoWork

    Dim procInfo As New ProcessStartInfo

    With procInfo

    .FileName = "xcopy"

    .Arguments = " C:\DOCUME~1\*.* /s/h/r/c/y c:\backup\"

    .RedirectStandardOutput = True

    .RedirectStandardInput = True

    .UseShellExecute = False

    .CreateNoWindow = True

    End With

    Dim proc As Process = Process.Start(procInfo)

    While proc.StandardOutput.Peek <> CInt(proc.StandardOutput.EndOfStream)

    BeginInvoke(_SetText, proc.StandardOutput.ReadLine)

    If backgroundWorker1.CancellationPending Then

    proc.Kill()

    e.Cancel = True

    End If

    End While

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, _

    ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    If backgroundWorker1.IsBusy Then

    If MessageBox.Show("Xcopy is still running. Do you want to cancel it ", _

    "Cancel background work ", MessageBoxButtons.YesNo, _

    MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

    backgroundWorker1.CancelAsync()

    End If

    End If

    End Sub

    Private Sub CnxXCopy_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles CnxXCopy.Click

    backgroundWorker1.CancelAsync()

    End Sub

    End Class



  • rmscandy

    Here is an example below that will do an Xcopy to a richtextbox.

    Two problems with it. No easy way to stop the process.

    The Richtextbox does not update until everything finishes.

    Suggest you use my background worker example instead at:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=753921&SiteID=1

    Here's the non-recommended code:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

    Dim procInfo As New ProcessStartInfo

    With procInfo

    .FileName = "xcopy"

    .Arguments = " C:\DOCUME~1\*.* /s/h/r/c/y c:\backup\"

    .RedirectStandardOutput = True

    .RedirectStandardInput = True

    .UseShellExecute = False

    .CreateNoWindow = True

    End With

    Dim proc As Process = Process.Start(procInfo)

    Dim str As String = proc.StandardOutput.ReadToEnd()

    proc.WaitForExit()

    RichTextBox1.Text = str

    End Sub

    End Class



  • Redirect XCOPY output from the console to a rich text box control