User interactions (like /p) no longer working when redirecting standardinput and output for cmd.exe

53 Views Asked by At

If I use the code down below to redirect standardinput and standardoutput to a textbox, everything is working, except for lines which require interactive input from the user. For instance, if you execute the command dir /p c:\windows, the whole content of the directory is displayed. The /p is not being honored. Same thing if you execute a script which requires user input. Here is a small example:

@echo off
set /p "id=Enter  ID: "
Echo The user ID entered is: %id%

When you execute this in a normal cmd window, the output is like this:

Enter  ID: MyUser
The user ID entered is: MyUser

But when I execute the same script through my form with redirected StandardInput and StandardOutput, the result looks like this:

Enter  ID: The user ID entered is: 

So here, the /p to prompt for input is also ignored. Is there any way to make this work the same way as in a standard cmd.exe windows? Thanks for any help in advance!

Kind Regards, Eric

Public Class Form1
    Dim WithEvents P As New Process
    Dim SW As System.IO.StreamWriter
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        P.EnableRaisingEvents = True
        Me.Text = "My title"
        AddHandler P.OutputDataReceived, AddressOf DisplayOutput
        P.StartInfo.CreateNoWindow() = True
        P.StartInfo.UseShellExecute = False
        P.StartInfo.RedirectStandardInput = True
        P.StartInfo.RedirectStandardOutput = True
        P.StartInfo.FileName = "cmd.exe"
        P.StartInfo.Arguments = ""
        P.Start()
        P.SynchronizingObject = Me
        P.BeginOutputReadLine()
        SW = P.StandardInput
        SW.WriteLine()
    End Sub
    Private Sub DisplayOutput(ByVal sendingProcess As Object, ByVal output As DataReceivedEventArgs)
        TextBox1.AppendText(output.Data() & vbCrLf)
    End Sub
    Private Sub Textbox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If e.KeyChar = Chr(Keys.Return) Then
            SW.WriteLine(TextBox2.Text)
        End If
    End Sub
    Private Sub myProcess_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles P.Exited
        Me.Close()
    End Sub End Class
0

There are 0 best solutions below