I need to be able to execute a DOS command, such as 'ipconfig', using a command line application in Visual Basic. I can simply use start.process("CMD", "ipconfig"), but that opens a new instance of CMD. I want to be able to run a command like I would with CMD, using a console application, without opening another CMD window. Thanks!
-
Do you mean on an already open CMD instance?Matteo NNZ– Matteo NNZ2015-01-07 19:30:01 +00:00Commented Jan 7, 2015 at 19:30
-
Yes :) I don't want more windows popping up, I just want it run in the original CMD application form.Sam Stenner– Sam Stenner2015-01-07 19:44:22 +00:00Commented Jan 7, 2015 at 19:44
-
I know what's the next question! "How can I read the results"SQLMason– SQLMason2015-01-07 20:31:15 +00:00Commented Jan 7, 2015 at 20:31
-
You can launch a command window from your console app but keep it invisible to the user... is this what you are looking for?laylarenee– laylarenee2015-01-07 20:56:18 +00:00Commented Jan 7, 2015 at 20:56
Add a comment
|
1 Answer
You can use this to run the ipconfig command in hidden console window and redirect the output to local variable. From here you can manipulate it as needed:
Dim cmdProcess As New Process
With cmdProcess
.StartInfo = New ProcessStartInfo("cmd.exe", "/C ipconfig")
With .StartInfo
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With
.Start()
.WaitForExit()
End With
' Read output to a string variable.
Dim ipconfigOutput As String = cmdProcess.StandardOutput.ReadToEnd
12 Comments
Matteo NNZ
It's not at all a bad idea, but I think he wants to get the output in his own command line application. At least this is what I have understood from the title.
Jason Faulkner
@MatteoNNZ - If so, just write the contents of
ipconfigOutput to the console.Sam Stenner
Matteo NNZ, yes, I would like to do what you said. Jason, I tried what you suggested and got this error: StandardOut has not been redirected or the process hasn't started yet.
Jason Faulkner
@SamStenner - I updated the answer. I forgot to add the
.RedirectStandardOutput = True under the .StartInfo.Chris Dunaway
I agree, but what I meant was that I took the code you posted and changed only the one line to:
.StartInfo = New ProcessStartInfo("ipconfig.exe") It works as expected. |