如何从 VB.NET 运行 DOS/CMD/命令提示符命令? [英] How to run DOS/CMD/Command Prompt commands from VB.NET?

查看:56
本文介绍了如何从 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

例如这样调用:

MyUtilities.RunCommandCom("DIR", "/W", true)

对于一行上的多个命令,关键是 &|&&和 ||命令连接器

For the multiple command on one line the key are the & | && and || command connectors

  • A &B →执行命令A,然后执行命令B.
  • A |B →执行命令 A,并将它的所有输出重定向到输入命令 B.
  • A & &B →执行命令A,运行后评估errorlevel命令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, evaluate the exit code of this command and if it's anything but 0, only then execute command B.

这篇关于如何从 VB.NET 运行 DOS/CMD/命令提示符命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆