Results 1 to 2 of 2

Thread: Passing cmd.exe to textbox, missing Prompt line

  1. #1

    Thread Starter
    Member The_Professional_05's Avatar
    Join Date
    Jul 2005
    Location
    Australia
    Posts
    36

    Passing cmd.exe to textbox, missing Prompt line

    Hello everyone,

    I have started a project to integrate a command prompt into a forms app.
    Borrowing code from here: http://stackoverflow.com/questions/1...e-using-vb-net
    Having a form, with a separate class to handle the new process.

    It is able to pass the StandardOutput to the text box, but it is missing the Prompt line, a.k.a the 'C:\>' at the end.
    If you pass a command to it using StandardInput, the textbox will then show the Prompt line with your command afterwards.

    But why not before? Is it due to that line not being 'complete' within cmd.exe, thus not being sent within the StandardOutput? Or would I have missed something in my code?

    Appreciate the help! (:

    Edit: Thought I would put in my code, so you know what I am working with

    Code:
    Public Class Form1
        Private WithEvents _commandExecutor As New CommandExecutor()
    
        Private Sub _commandExecutor_OutputRead(output As String) Handles _commandExecutor.OutputRead
            'If Not Me.Tag = "Disposing" Then 
            Me.Invoke(New processCommandOutputDelegate(AddressOf processCommandOutput), output)
        End Sub
    
        Private Delegate Sub processCommandOutputDelegate(output As String)
        Private Sub processCommandOutput(output As String)
            text_output.Text += output + vbCrLf
            text_output.SelectionStart = text_output.TextLength
            text_output.ScrollToCaret()
        End Sub
    
        Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            _commandExecutor.Dispose()
            'Me.Tag = "Disposing"
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            _commandExecutor.Execute("C:\Windows\system32\cmd.exe", "")
            text_command.Focus()
        End Sub
    
        Private Sub button_execute_Click(sender As Object, e As EventArgs) Handles button_execute.Click
            _commandExecutor.SendCommand(text_command.Text)
            text_command.Text = ""
            text_command.Focus()
        End Sub
    End Class
    
    Public Class CommandExecutor
        Implements IDisposable
    
        Public Event OutputRead(output As String)
    
        Private WithEvents _process As Process
    
        Public Sub Execute(filePath As String, arguments As String)
            If _process IsNot Nothing Then
                Throw New Exception("Already watching process")
            End If
            _process = New Process()
            _process.StartInfo.FileName = filePath
            _process.StartInfo.Arguments = arguments
            _process.StartInfo.UseShellExecute = False
            '_process.StartInfo.LoadUserProfile = True
            _process.StartInfo.CreateNoWindow = True
            _process.StartInfo.RedirectStandardInput = True
            _process.StartInfo.RedirectStandardOutput = True
            '_process.StartInfo.RedirectStandardError = True
            '_process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
            _process.Start()
            _process.BeginOutputReadLine()
        End Sub
    
        Public Sub SendCommand(cmdstring As String)
            _process.StandardInput.WriteLine(cmdstring)
        End Sub
    
        Private Sub _process_OutputDataReceived(sender As Object, e As DataReceivedEventArgs) Handles _process.OutputDataReceived
            If _process.HasExited Then
                _process.Dispose()
                _process = Nothing
            End If
            RaiseEvent OutputRead(e.Data)
        End Sub
    
        Private disposedValue As Boolean = False
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    If _process IsNot Nothing Then
                        _process.Kill()
                        _process.Dispose()
                        _process = Nothing
                    End If
                End If
            End If
            Me.disposedValue = True
        End Sub
    
        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
    End Class
    Last edited by The_Professional_05; Feb 20th, 2014 at 04:21 AM.
    MCP, MCDST, A+, CSSA, ACFE, MCTS EX2010
    VB5, VB2013, HTML, CSS, ASP, ASP.NET, ADO, MS-SQL, PowerShell

  2. #2

    Thread Starter
    Member The_Professional_05's Avatar
    Join Date
    Jul 2005
    Location
    Australia
    Posts
    36

    Unhappy Re: Passing cmd.exe to textbox, missing Prompt line

    anyone?
    MCP, MCDST, A+, CSSA, ACFE, MCTS EX2010
    VB5, VB2013, HTML, CSS, ASP, ASP.NET, ADO, MS-SQL, PowerShell

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width