|
-
Jan 16th, 2017, 12:04 PM
#1
Thread Starter
Lively Member
CMD CommanderAutomate
My goal is to have my program run any command through CMD in the background and then display the results in a textbox. So far this program is functioning alright until it gets to a command that has a long response. For example, if I try to run "tasklist /m", my reply is blank because its too long. I am guessing this is the reason why my response is coming back with nothing since smaller codes such as just "tasklist" work. I have tried increasing the property for maxlength and going from a typical textbox to a richtextbox. Neither of those two changes solved my problem.
On my computer, the "tasklist /m" command has 68,391 char.
Here is my code that I am using. I would like to keep the same code with just a few adjustments, but if that isn't possible, I can make whatever work. I am just in need of any advice or complete help. 
Code:
Public Class Form1
Private Results As String
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)
Private Sub UpdateText()
txtResults.Text = Results
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub
Private Sub CMDAutomate()
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True 'creates no cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine(txtCommand.Text) 'the command you wish to run.....
SW.WriteLine("exit") 'exits command prompt window
Results = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
'invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
If you would rather, you can download the attachment. 2010CMDExample.zip
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|