这个问题很明确。如果代码只有一行最好(可能与“Process.Start("...")
”有关?)。我查阅了网络,但只找到旧的例子和不起作用的例子(至少对我是这样)。我想在我的类库中使用它来运行Git命令(如果有帮助的话?)。
这个问题很明确。如果代码只有一行最好(可能与“Process.Start("...")
”有关?)。我查阅了网络,但只找到旧的例子和不起作用的例子(至少对我是这样)。我想在我的类库中使用它来运行Git命令(如果有帮助的话?)。
你可以尝试这种方法:
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)
编辑: 对于将多个命令放在同一行上的情况,关键在于 & | && 和 || 命令连接符
你可以尝试这个方法来运行命令,然后cmd
退出。
Process.Start("cmd", "/c YourCode")
您可以尝试此方法来运行命令,并让cmd
等待更多命令。
Process.Start("cmd", "/k YourCode")
MyUtilities.RunCommandCom("DIR", "/W", true)
我实际上可以直接在我的字符串中键入命令并调用它,像这样...
直接在代码中。
Call "CD %APPDATA% & TREE".RunCMD()
或者
从变量中。
Dim MyCommand = "CD %APPDATA% & TREE"
MyCommand.RunCMD()
OR
来自文本框。
textbox.text.RunCMD(WaitForProcessComplete:=True)
扩展方法需要放置在一个公共模块中,并在子过程上带有<Extension>
特性。您还需要在代码文件的顶部添加Imports System.Runtime.CompilerServices
。
如果需要进一步帮助,SO上有大量关于扩展方法的信息。
扩展方法
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
Sub systemcmd(ByVal cmd As String)
Shell("cmd /c """ & cmd & """", AppWinStyle.MinimizedFocus, True)
End Sub
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
这也可以正常工作