0% found this document useful (0 votes)
461 views

How To Run DOS - CMD - Command Prompt Commands From VB - Net - Stack Overflow

This document discusses how to run DOS/Command Prompt commands from VB.NET. It provides an example of using the Process.Start method to launch the command line with a command like DIR or MSPaint. It also describes how to run multiple commands by using command connectors like & to run one command then another, or || to run a second command if the first fails. The answers suggest using Process.Start with the "/c" or "/k" parameters to either close or keep the command prompt open after running commands.

Uploaded by

me.ultravirus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
461 views

How To Run DOS - CMD - Command Prompt Commands From VB - Net - Stack Overflow

This document discusses how to run DOS/Command Prompt commands from VB.NET. It provides an example of using the Process.Start method to launch the command line with a command like DIR or MSPaint. It also describes how to run multiple commands by using command connectors like & to run one command then another, or || to run a second command if the first fails. The answers suggest using Process.Start with the "/c" or "/k" parameters to either close or keep the command prompt open after running commands.

Uploaded by

me.ultravirus
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

How to run DOS/CMD/Command Prompt commands from VB.NET? -...

http://stackoverflow.com/questions/10261521/how-to-run-dos-cmd-c...

sign up

log in

tour

help

careers 2.0

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to run DOS/CMD/Command Prompt commands from VB.NET?


The question is self-explanatory. It would be great if the code was one line long (something to do with " Process.Start("...") "?). I researched the web but only found old examples and such ones that do not work (at least for me). I want to use this in my class library, to run Git commands (if that helps?).
vb.net cmd command

edited Feb 8 at 15:10 Tshepang 2,497 7 31 72

asked Apr 21 '12 at 17:55 user1072207

maybe start simple with Process.Start("C:\Windows\System32\MSPaint.exe") - then once you get that going start trying the Git commands I imagine there much harder with all the additional command line arguments. The other useful tip to help you work out whats wrong, is to read the StandardError, the answer here is a perfect example: stackoverflow.com/questions/2709198/ Jeremy Thompson Apr 22 '12 at 0:41

2 Answers
You could try this method: Public Class MyUtilities Shared Sub RunCommandCom(command as String, arguments as String, permanent as Boolean Dim p as Process = new Process() Dim pi as ProcessStartInfo = new ProcessStartInfo() pi.Arguments = " " + if(permanent = true, "/K" , "/C") + " " + command + " " + arguments pi.FileName = "cmd.exe" p.StartInfo = pi p.Start() End Sub End Class call, for example, in this way: MyUtilities.RunCommandCom("DIR", "/W", true) EDIT: For the multiple command on one line the key are the & | && and || command connectors A & B -> execute command A, then execute command B. A | B -> execute command A, and redirect all it's output into the input of command B. A && B -> execute command A, evaluate the errorlevel after running Command A, and if the exit code (errorlevel) is 0, only then execute command B. A || B -> execute Command A, evalutate the exit code of this command and if it's anything but 0, only then execute command B.
edited Apr 22 '12 at 16:58 answered Apr 21 '12 at 21:26 Steve 77k 9 26 71

Hello! I used this code (gist.github.com/2465024) and when I run your command (I changed the name to " runCommand " and used that - so that's not the problem!), I get a " Reference to a non-shared member requires an object reference. " error. Any ideas why? user1072207 Apr 22 '12 at 16:18 BTW, it works when it's in the same class, however I used it in a Class Library, which I imported to my Windows Forms project. Any ideas why it's not working? user1072207 Apr 22 '12 at 16:22 Also, is there a way to run multiple commands at once. Something like " Hide cmd, run first command, hide cmd, run second command, show cmd. ". user1072207 Apr 22 '12 at 16:27 For the class library problem: Yes, of course, inside a class library you need to declare the method Shared if you want to use without creating an instance. I will edit the answer. Steve Apr 22 '12 at 16:46 For the multiple commands at once, see my updated answer. Steve Apr 22 '12 at 17:01 show 1 more comment

1 of 2

24-03-2014 07:01

How to run DOS/CMD/Command Prompt commands from VB.NET? -...

http://stackoverflow.com/questions/10261521/how-to-run-dos-cmd-c...

' You Can Use This To Run Command Then Cmd Exits Process.Start("cmd", "/c YourCode") ' You Can Use This To Run The Command And Let Cmd Wait For More Commands Process.Start("cmd", "/k YourCode")
answered Aug 28 '13 at 20:12 Saeed A Suleiman 21 1

2 of 2

24-03-2014 07:01

You might also like