using cmd.exe from my application

duall

Member
Joined
Jun 12, 2007
Messages
13
Programming Experience
Beginner
well, i need help to make cmd.exe usable from my application it should look like this

i want to send commands to cmd.exe..
also I want to receive cmd.exe text into textbox1 it should look something like:
C:\Documents and Settings\user>
'test' is not recognized as an
operable program or batch file

C:\Documents and Settings\user>


i'm also trying to google somethin, but no luck..
 
The console output can be redirected by setting the RedirectStandardOutput as given VB.Net example in documentation: http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
"cmd.exe" will be the application name, arguments you have to specify separate, either with the ProcessStartInfo constructor:
VB.NET:
Dim myProcessStartInfo As New ProcessStartInfo("cmd.exe", "/c some arguments")
or setting the Arguments property of the ProcessStartInfo instance:
VB.NET:
myProcessStartInfo.Arguments = "/c some arguments"
Note that you don't have to route standalone applications that output to console through the cmd.exe, only those commands that are provided by the cmd.exe itself, type "help" in command prompt to see what those are. (DIR and such). You also might have to set myProcessStartInfo.StandardOutputEncoding if you're getting strange characters.
 
i'm having trouble with argument ping blabla.com -t
it just hangs my application, i want to kill cmd.exe after 10sec when it was launched....
 
what if i want to use cd D:\ then cd blabla then DIR

myProcessStartInfo.Arguments = "/c some arguments" is only for one argument and after a new argument is entered my application forgets previous arguments...

this is how my program looks like
cmd.jpg
 
how to scroll textbox to the bottom everytime it changes??
something like this:
VB.NET:
TextBox1.SelectionStart = TextBox1.Text.Length
i'm having trouble with argument ping blabla.com -t
it just hangs my application, i want to kill cmd.exe after 10sec when it was launched....
Ping.exe is an application of its own, it is not an integrated command of the cmd.exe command interpreter, so you would normally run this executable as the process giving it the arguments you choose from what is available. Using the -t switch requires user interaction, you may want to reconsider if you really want the ping to run for an indefinite time (-n and -w switches may be better suited). Else look into redirecting standard input with the RedirectStandardInput property. Above that, StreamReader.ReadToEnd method will not return until end of stream is reached, ie when process is finished, so for such cases you have to use the StreamReader.ReadLine method and check if returned string from each call Is Nothing.
what if i want to use cd D:\ then cd blabla then DIR
In this case you use the path argument of DIR, issuing multiple commands in this case is redundant. "cmd.exe", "/c dir d:\".

In the event of carrying out a batch set you could generate a batch file, which is a plain text file with each command on its own line, it uses the .bat file extension and is an executable file by itself.

You could also use multiple arguments of the "/c" switch according to the documentation:
Note that multiple commands separated by the command separator '&&' are accepted for string if surrounded by quotes.
 
Also don't forget .Net Framework have many classes that might do these tasks much better than running commands through the ancient "DOS" shell. For file listings and such you have the System.IO namespace, and there is the System.Net.NetworkInformation.Ping class.
 
well i'm making a gprs PC remote tool for my mobile :) so i want to make an ancient "DOS" function in my phone...but first i need to make a working command promit server on PC..
 
myProcess.Kill()
 
Back
Top