如何从VB.NET运行DOS / CMD /命令提示符命令? [英] How to run DOS/CMD/Command Prompt commands from VB.NET?
本文介绍了如何从VB.NET运行DOS / CMD /命令提示符命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题是不言而喻的。如果代码是一行长的(这与 Process.Start(...)
有关)。我研究了网络,但只发现了旧的例子和那些不工作(至少对我来说)。我想在我的类库中使用这个,运行Git命令(如果这有帮助。)。
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?).
推荐答案
方法:
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
- B - >执行命令A,然后执行命令B.
- B - >执行命令A,并将其所有输出重定向到命令B的
输入。 - A& B - >执行命令A,在运行
命令A后评估错误级别,如果退出代码(errorlevel)为0,则只执行
命令B. - A || B - >执行命令A,评估此命令的退出代码
,如果它不是0,则只执行命令B.
- 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.
这篇关于如何从VB.NET运行DOS / CMD /命令提示符命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文