Results 1 to 40 of 61

Thread: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

Threaded View

  1. #11

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application

    Here is the updated code, with a delegate used to update the text. Tested in 2005 and the code worked fine. There is an attachment of the new 2005 project example at the end of the post as well.
    VB Code:
    1. 'Form code from sample project
    2.  
    3.     Private Results As String
    4.     Private Delegate Sub delUpdate()
    5.     Private Finished As New delUpdate(AddressOf UpdateText)
    6.  
    7.     Private Sub UpdateText()
    8.         txtResults.Text = Results
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
    13.         CMDThread.Start()
    14.     End Sub
    15.  
    16.     Private Sub CMDAutomate()
    17.         Dim myprocess As New Process
    18.         Dim StartInfo As New System.Diagnostics.ProcessStartInfo
    19.         StartInfo.FileName = "cmd" 'starts cmd window
    20.         StartInfo.RedirectStandardInput = True
    21.         StartInfo.RedirectStandardOutput = True
    22.         StartInfo.UseShellExecute = False 'required to redirect
    23.         StartInfo.CreateNoWindow = True 'creates no cmd window
    24.         myprocess.StartInfo = StartInfo
    25.         myprocess.Start()
    26.         Dim SR As System.IO.StreamReader = myprocess.StandardOutput
    27.         Dim SW As System.IO.StreamWriter = myprocess.StandardInput
    28.         SW.WriteLine(txtCommand.Text) 'the command you wish to run.....
    29.         SW.WriteLine("exit") 'exits command prompt window
    30.         Results = SR.ReadToEnd 'returns results of the command window
    31.         SW.Close()
    32.         SR.Close()
    33.         'invokes Finished delegate, which updates textbox with the results text
    34.         Invoke(Finished)
    35.     End Sub
    Attached Files Attached Files
    Last edited by gigemboy; Sep 12th, 2006 at 12:46 PM.

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