Start process and redirect output

Function GetProcessText(ByVal process As String, ByVal param As String, _
        ByVal workingDir As String) As String
    Dim p As System.Diagnostics.Process = New System.Diagnostics.Process

    ' this is the name of the process we want to execute 
    p.StartInfo.FileName = process
    If Not (workingDir = "") Then
        p.StartInfo.WorkingDirectory = workingDir
    End If
    p.StartInfo.Arguments = param

    ' need to set this to false to redirect output
    p.StartInfo.UseShellExecute = False
    p.StartInfo.RedirectStandardOutput = True

    ' start the process 
    p.Start()

    ' read all the output
    ' here we could just read line by line and display it
    ' in an output window 
    Dim output As String = p.StandardOutput.ReadToEnd
    ' wait for the process to terminate 
    p.WaitForExit()
    Return output
End Function

Code from http://www.developerfusion.co.uk/show/4662/.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.