问题是不言自明的。如果代码长一行(与“Process.Start("...")
”有关)会很棒吗?)。我研究了网络,但只找到了旧的例子和那些不起作用的例子(至少对我而言)。我想在我的类库中使用它来运行Git命令(如果这有帮助吗?)。
答案 0 :(得分:18)
你可以尝试这种方法:
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)
编辑:对于一行上的多个命令,键是& | &安培;&安培;和||命令连接器
答案 1 :(得分:13)
您可以尝试运行此命令然后cmd
退出
Process.Start("cmd", "/c YourCode")
您可以尝试运行此命令并让cmd
等待更多命令
Process.Start("cmd", "/k YourCode")
答案 2 :(得分:3)
我的灵感来自史蒂夫的答案,但我想我会为它添加一些亮点。 我喜欢在编写扩展方法之前做好工作,以便稍后调用方法的工作量减少。
例如,使用下面史蒂夫的答案的修改版本,而不是拨打这个电话......
MyUtilities.RunCommandCom("DIR", "/W", true)
我实际上只需输入命令并从我的字符串中调用它......
直接在代码中。
Call "CD %APPDATA% & TREE".RunCMD()
OR
来自变量。
Dim MyCommand = "CD %APPDATA% & TREE"
MyCommand.RunCMD()
OR
来自文本框。
textbox.text.RunCMD(WaitForProcessComplete:=True)
扩展方法需要放在公共模块中,并在子上携带<Extension>
属性。您还需要将Imports System.Runtime.CompilerServices
添加到代码文件的顶部。
如果您需要进一步的帮助,有很多关于扩展方法的信息。
扩展方法
Public Module Extensions
''' <summary>
''' Extension method to run string as CMD command.
''' </summary>
''' <param name="command">[String] Command to run.</param>
''' <param name="ShowWindow">[Boolean](Default:False) Option to show CMD window.</param>
''' <param name="WaitForProcessComplete">[Boolean](Default:False) Option to wait for CMD process to complete before exiting sub.</param>
''' <param name="permanent">[Boolean](Default:False) Option to keep window visible after command has finished. Ignored if ShowWindow is False.</param>
<Extension>
Public Sub RunCMD(command As String, Optional ShowWindow As Boolean = False, Optional WaitForProcessComplete As Boolean = False, Optional permanent As Boolean = False)
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
pi.Arguments = " " + If(ShowWindow AndAlso permanent, "/K", "/C") + " " + command
pi.FileName = "cmd.exe"
pi.CreateNoWindow = Not ShowWindow
If ShowWindow Then
pi.WindowStyle = ProcessWindowStyle.Normal
Else
pi.WindowStyle = ProcessWindowStyle.Hidden
End If
p.StartInfo = pi
p.Start()
If WaitForProcessComplete Then Do Until p.HasExited : Loop
End Sub
End Module
答案 3 :(得分:1)
Sub systemcmd(ByVal cmd As String)
Shell("cmd /c """ & cmd & """", AppWinStyle.MinimizedFocus, True)
End Sub
答案 4 :(得分:0)
Imports System.IO
Public Class Form1
Public line, counter As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
counter += 1
If TextBox1.Text = "" Then
MsgBox("Enter a DNS address to ping")
Else
'line = ":start" + vbNewLine
'line += "ping " + TextBox1.Text
'MsgBox(line)
Dim StreamToWrite As StreamWriter
StreamToWrite = New StreamWriter("C:\Desktop\Ping" + counter + ".bat")
StreamToWrite.Write(":start" + vbNewLine + _
"Ping -t " + TextBox1.Text)
StreamToWrite.Close()
Dim p As New System.Diagnostics.Process()
p.StartInfo.FileName = "C:\Desktop\Ping" + counter + ".bat"
p.Start()
End If
End Sub
End Class
这也适用