0

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!

4
  • Do you mean on an already open CMD instance? Commented 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. Commented Jan 7, 2015 at 19:44
  • I know what's the next question! "How can I read the results" Commented 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? Commented Jan 7, 2015 at 20:56

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

12 Comments

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.
@MatteoNNZ - If so, just write the contents of ipconfigOutput to the console.
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.
@SamStenner - I updated the answer. I forgot to add the .RedirectStandardOutput = True under the .StartInfo.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.