How do I redirect the standard I/O of a process?

Locked How do I redirect the standard I/O of a process?

Locked

  • Friday, April 10, 2009 8:47 AM
     
      Has Code

    How do I redirect the standard I/O of a process?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Friday, April 10, 2009 8:47 AM
     
     Answered

    You need to use the Process class and set its StandardInput and StandardOutput properties to implement this operation. Please check the following code snippet.

     

    Imports System.IO
    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim p As New Process()
            Dim sw As StreamWriter
            Dim sr As StreamReader
            Dim err As StreamReader
            Dim psI As New ProcessStartInfo("cmd")
            psI.UseShellExecute = False
            psI.RedirectStandardInput = True
            psI.RedirectStandardOutput = True
            psI.RedirectStandardError = True
            psI.CreateNoWindow = True
            p.StartInfo = psI
            p.Start()
            sw = p.StandardInput
            sr = p.StandardOutput
            err = p.StandardError
            sw.AutoFlush = True
            If TextBox3.Text <> "" Then
                sw.WriteLine(TextBox3.Text)
            Else
                'execute default command 
                sw.WriteLine("dir \")
            End If
            sw.Close()
            TextBox1.Text = sr.ReadToEnd()
            TextBox1.Text += err.ReadToEnd()
        End Sub
    End Class 
    
    

     

    Read the following links for detailed information. 

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/62acea83-236b-4f55-8eec-25d80df5df66/

    http://www.c-sharpcorner.com/UploadFile/edwinlima/SystemDiagnosticProcess12052005035444AM/SystemDiagnosticProcess.aspx

     

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Friday, April 10, 2009 8:47 AM
     
     
    Do tell!
    Doug [Remember to mark replies as helpful (if they are) or as the answer]
    • Proposed As Answer by Doug__ Friday, April 10, 2009 8:48 AM
    • Unproposed As Answer by Martin Xie - MSFT Friday, April 10, 2009 10:31 AM
    •