I needed to do some cmd stuff today and found this thread useful. I ended up embedding the example in this post into a class which makes it easy to use. At least I think so...
Code:
Class CMDThread
Public Param As String
Public isFinished As Boolean = False
Private tr As Thread = Nothing
Private results As String = ""
Private myprocess As New Process
Private StartInfo As New System.Diagnostics.ProcessStartInfo
Public Function Start() As Thread
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()
tr = New Thread(AddressOf Me.work)
tr.Start()
Return tr
End Function
Public Sub Join()
tr.Join()
End Sub
Public Function GetOutput() As String
GetOutput = results
results = "" 'Not sure if this is safe while thread is executing... seem to work
End Function
Private Sub work()
myprocess.StandardInput.WriteLine(Param & vbCrLf & "exit") 'the command you wish to run, with an exit at the end to terminate process after run
While myprocess.StandardOutput.EndOfStream = False
results += myprocess.StandardOutput.ReadLine() & vbCrLf
End While
isFinished = True
End Sub
End Class
To launch a couple of threads and use join to wait for the last one:
Code:
Dim threadA, threadB As New CMDThread
Dim res As String = ""
threadA.Param = "ping -n 2 127.0.0.1"
threadB.Param = "tracert -h 10 -d -w 30 vbforums.com"
threadA.Start()
threadB.Start()
While Not threadA.isFinished
res = threadA.GetOutput
...do stuff...
End While
'A is finished
If Not threadB.isFinished Then
threadB.Join() 'wait until B is finished
End If
res = threadB.GetOutput
This thread has been a huge help to me. I wanted to do something very simple with the CMD prompt that would save me a ton of time. I busted out my 10 year old VB book and felt a bit overwhelmed about how much I had forgotten.
Thanks for the code, it rules. I am having one small snytax issue which I'm sure you guys could knock out of the park.
I am trying to use the txt box to define a variable, then run some commands using that variable. This will be used to check SRV records for SIP domains.
So "nslookup -quertype=srv _h323ls._udp.SIPDOMAIN" Where SIPDOMAIN is pulled from the txtcommand box.
Code:
Private Sub CMDAutomate()
Dim sipdomain As TextBox = txtCommand
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("nslookup -querytype=srv _h323ls._udp.", &sipdomain) 'the command you wish to run.....
SW.WriteLine("nslookup -querytype=srv _h323cs._tcp.", sipdomain) 'the command you wish to run.....
SW.WriteLine("nslookup -querytype=srv _sip._tcp.", sipdomain) 'the command you wish to run.....
SW.WriteLine("nslookup -querytype=srv _sip._udp.", sipdomain) 'the command you wish to run.....
SW.WriteLine("nslookup -querytype=srv _sips._tcp.", sipdomain) '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
Also it seems that it hangs after one run and I have to restart to run a 2nd command each time.
Thank you for the great little piece of code I have a project that currently pops the cmd and bugs the crap out of me. I took the code for VS10 and created a new project for VS11 for what ever reason the VS10 example would not open the solution or project I tested it with dir, netdom, and a couple others all seem to work as the code was written for. It should be attached to this post.
Re: Automate Command Prompt Window (CMD), Redirect Output to Application
Originally Posted by gigemboy
The below code adds one line, with the "CreateNoWindow" property of the StartInfo object set to "True", this way the command window doesnt open up at all... (Thanks to jmcilhinney)
VB Code:
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 window, obviously
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()
i'm nt able to get the result of the command window on my form
Dim sf As New System.IO.StreamWriter(TextBox2.Text & ".java")
sf.Write(RichTextBox1.Text)
sf.Close()
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CheckForIllegalCrossThreadCalls = False
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
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine("set path=C:\Program Files\Java\jdk1.7.0\bin") 'the command you wish to run.....
SW.WriteLine("cd C:\Users\sanjeev\Desktop\sanju\SEM-VII\Cloud\javacodes\") 'the command you wish to run.....
SW.WriteLine(path) 'the command you wish to run.....
MsgBox(path)
StartInfo.FileName = "cmd" 'starts cmd window
SW.WriteLine(runpath)
'RichTextBox2.Text = SR.ReadToEnd 'returns results of the command window
SW.WriteLine("exit")
RichTextBox2.Text = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
End Sub
while debugging i wrote a java code and saved it and have run it
and i got the following output:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\sanjeev\Documents\Visual Studio 2010\Projects\WindowsApplication2\WindowsApplication2\bin\Debug>set path=C:\Program Files\Java\jdk1.7.0\bin
C:\Users\sanjeev\Documents\Visual Studio 2010\Projects\WindowsApplication2\WindowsApplication2\bin\Debug>cd C:\Users\sanjeev\Desktop\sanju\SEM-VII\Cloud\javacodes\
Hey, I am having the same issue as bvrider1 and Sanjeev467.
No matter what I do, even when I just download the zip files and run them without changing them at all. I can enter commands into the box and press the button, but nothing happens at all. The text box remains blank.
I run VB10 on VS10. All I want to do is gather information from a cmd window without using a text file. But when I try the above approaches, it seems like the readtoend fails or something... My variable remains blank.
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
' Dim mycmd As String = "net use \\172.x.x.x /user:mynetwork\"
Dim mycmd As String = "net use \\172.x.x.x\IPC$ /user:mynetwork\"
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'used for redirect
StartInfo.CreateNoWindow = True 'hides 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(txtInputBox.Text + " " + txtPWbox.Text) 'the command you wish to run.....
SW.WriteLine(mycmd + txtInputBox.Text + " " + txtPWbox.Text)
SW.WriteLine("Hello - Your drive is now mapped")
SW.WriteLine(txtPWbox)
Results = SR.ReadToEnd
SW.Close()
SR.Close()
Invoke(Finished)
End Sub
Last edited by bvrider1; Sep 20th, 2012 at 08:39 PM.
I was using net use to map a drive. I had the user enter data in textbox1 and textbox2 and using the redirects finished the command line portion of it.
Basically, the variable is blank no matter what. What does Invoke(Finished) do? That's the only part of your code I haven't used. I just let the sub end at SR.Close()... I wonder if that's the issue... If not, I have no idea what I am doing wrong.
It appears that my variable, after going through any of the above code will be set to blank. I get the feeling that my CMD window is just blank at the start and blank during and after I give it commands. Anyone know why?
Basically, the variable is blank no matter what. What does Invoke(Finished) do? That's the only part of your code I haven't used. I just let the sub end at SR.Close()... I wonder if that's the issue... If not, I have no idea what I am doing wrong.
It appears that my variable, after going through any of the above code will be set to blank. I get the feeling that my CMD window is just blank at the start and blank during and after I give it commands. Anyone know why?
It's been a few months now and I have fixed my issue. Basically, I was programming in a weird enterprise environment and I didn't have enough permissions to do what I wanted to. So here's my fix: to get the code to work correctly, first, I build the project. Then, I go to my project's bin/debug folder and copy the .exe of my project. I paste that .exe into my C:\temp folder. THEN from my desktop I right click run as admin a .bat file that I created. This .bat file contains nothing but "start C:\temp\filename.exe"
When I call the program like that, I am able to circumvent the administrative difficulties that I was experiencing. I hope writing this saves some other newbie days of fiddling.
Hi thanks, for this code, is exactly what I was looking for! I know it's been a while since last message, but I need some help here:
my intent is to execute an external script that runs some commands, this code is working fine when it as to invoke the external script and the script do its stuff just fine, only problem is I don't receive the updated output from the cmd. I'd like that the textbox shows each step in wich external script is.
Right now what the external script is doing is showed only at the end of its run, but I need realtime updates. Is this doable?
Thanks a lot, I hope it's clear enough