I'm Developing application in VB.NEt(Visual studio 2017) I wan to send some commands to CMD and receive output in multiline textbox How can i do it? and only one window of cmd should be visible
Asked
Active
Viewed 3,846 times
-1
-
I suggest that you should run a batch file instead from vb – preciousbetine Nov 25 '18 at 05:30
-
1Check this out: http://www.vbforums.com/showthread.php?381405 – jmcilhinney Nov 25 '18 at 05:56
-
Possible duplicate of [Execute command promt process asnyc and get result](https://stackoverflow.com/questions/7804725/execute-command-promt-process-asnyc-and-get-result) – Visual Vincent Nov 25 '18 at 09:13
-
[How do I get output from a command to appear in a control on a form in real-time?](https://stackoverflow.com/questions/51680382/how-do-i-get-output-from-a-command-to-appear-in-a-control-on-a-form-in-real-time?answertab=active#tab-top). There's a sample project attached. The code is C#, but it's quite simple to read/translated and the notes there could be useful. – Jimi Nov 25 '18 at 15:48
-
This one is in VB.Net language [How To Output Shell Command To a RichTextBox In Visual Basic](https://stackoverflow.com/questions/51312990/how-to-output-shell-command-to-a-richtextbox-in-visual-basic?answertab=active#tab-top). – Jimi Nov 25 '18 at 15:52
2 Answers
3
A bit of googling...
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.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.CreateNoWindow = True '<---- if you want to not create a window
StartInfo.UseShellExecute = False 'required to redirect
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
txtResults.Text = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
End Sub

yossef douieb
- 146
- 1
- 2
- 12
2
the previous answer is nearly correct but will bring up a cross-thread error. Put the return text from the cmd output into a variable and then call the string after the Sub in the button click as below...
on a form create a button called: btnPowerShSend, create two text boxes with the multi-line property ticked, called: txtResults & txtScript
Public Class Form1
Dim retText As String
Private Sub BtnPowerShSend_Click(sender As Object, e As EventArgs) Handles btnPowerShSend.Click
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
txtResults.Text = retText 'returns results of the command window
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.CreateNoWindow = True '<---- if you want to not create a window
StartInfo.UseShellExecute = False 'required to redirect
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine(txtScript.Text) 'the command you wish to run.....
SW.WriteLine("exit") 'exits command prompt window
retText = SR.ReadToEnd.ToString
SW.Close()
SR.Close()
End Sub
End Class

Ranger1234
- 31
- 5