如何使用Visual Basic代码运行命令提示符命令?

5

我正在尝试使用Visual Basic编码,更具体地说是Visual Studio 2010。我希望通过点击按钮,让我的程序执行一个命令。这是否可能?

4个回答

9

这里有一个例子:

Process.Start("CMD", "/C Pause")


/C  Carries out the command specified by string and then terminates
/K  Carries out the command specified by string but remains

以下是一个扩展函数: (注意使用CMD命令的注释行。)

#Region " Run Process Function "

' [ Run Process Function ]
'
' // By Elektro H@cker
'
' Examples :
'
' MsgBox(Run_Process("Process.exe")) 
' MsgBox(Run_Process("Process.exe", "Arguments"))
' MsgBox(Run_Process("CMD.exe", "/C Dir /B", True))
' MsgBox(Run_Process("CMD.exe", "/C @Echo OFF & For /L %X in (0,1,50000) Do (Echo %X)", False, False))
' MsgBox(Run_Process("CMD.exe", "/C Dir /B /S %SYSTEMDRIVE%\*", , False, 500))
' If Run_Process("CMD.exe", "/C Dir /B", True).Contains("File.txt") Then MsgBox("File found")

Private Function Run_Process(ByVal Process_Name As String, _
                             Optional Process_Arguments As String = Nothing, _
                             Optional Read_Output As Boolean = False, _
                             Optional Process_Hide As Boolean = False, _
                             Optional Process_TimeOut As Integer = 999999999)

    ' Returns True if "Read_Output" argument is False and Process was finished OK
    ' Returns False if ExitCode is not "0"
    ' Returns Nothing if process can't be found or can't be started
    ' Returns "ErrorOutput" or "StandardOutput" (In that priority) if Read_Output argument is set to True.

    Try

        Dim My_Process As New Process()
        Dim My_Process_Info As New ProcessStartInfo()

        My_Process_Info.FileName = Process_Name ' Process filename
        My_Process_Info.Arguments = Process_Arguments ' Process arguments
        My_Process_Info.CreateNoWindow = Process_Hide ' Show or hide the process Window
        My_Process_Info.UseShellExecute = False ' Don't use system shell to execute the process
        My_Process_Info.RedirectStandardOutput = Read_Output '  Redirect (1) Output
        My_Process_Info.RedirectStandardError = Read_Output ' Redirect non (1) Output
        My_Process.EnableRaisingEvents = True ' Raise events
        My_Process.StartInfo = My_Process_Info
        My_Process.Start() ' Run the process NOW

        My_Process.WaitForExit(Process_TimeOut) ' Wait X ms to kill the process (Default value is 999999999 ms which is 277 Hours)

        Dim ERRORLEVEL = My_Process.ExitCode ' Stores the ExitCode of the process
        If Not ERRORLEVEL = 0 Then Return False ' Returns the Exitcode if is not 0

        If Read_Output = True Then
            Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Error Output (If any)
            Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Standard Output (If any)
            ' Return output by priority
            If Process_ErrorOutput IsNot Nothing Then Return Process_ErrorOutput ' Returns the ErrorOutput (if any)
            If Process_StandardOutput IsNot Nothing Then Return Process_StandardOutput ' Returns the StandardOutput (if any)
        End If

    Catch ex As Exception
        'MsgBox(ex.Message)
        Return Nothing ' Returns nothing if the process can't be found or started.
    End Try

    Return True ' Returns True if Read_Output argument is set to False and the process finished without errors.

End Function

#End Region

5
是的。您可以使用 Process.Start 来启动一个可执行程序,包括控制台应用程序。
如果您需要读取应用程序的输出,则可能需要从其StandardOutput流中读取信息,以获取从启动的应用程序中打印出来的任何内容。

好的。我启动了cmd.exe进程,但是如何让它执行命令呢?你能给我展示一些例子吗?我找不到它。谢谢! - Fellah
@Fellah 只需启动您想要的命令,而不是cmd.exe。您可以将其指定为args,但如果您想要一个详细的示例,则需要显示一些您尝试运行的命令的详细信息... - Reed Copsey
好的。假设 cd C:\ 是我的代码,它只是 Process.Start("cmd.exe") 并且它可以工作,但当它是 Process.Start("cd C:") 时,会出现错误。感谢您花时间看我的东西,伙计。 - Fellah
@Fellah,你需要实际输入你想要运行的“真实命令”,但它应该是 Process.Start("someApp.exe", "arg1 arg2") - Reed Copsey
@Fellah 你没有说明你要运行什么。运行cmd.exe并不是很有用 - 你实际想要执行的是哪个应用程序?你想运行的是哪个“DOS命令”? - Reed Copsey
显示剩余3条评论

5
或者,您可以采用非常简单的方式来处理。
Dim OpenCMD 
OpenCMD = CreateObject("wscript.shell")
OpenCMD.run("Command Goes Here")

0

1
这与Visual Basic有什么关系? - Reed Copsey
哦,我没看到那个。我只看到了“更具体地说是Visual Studio 2010”。我的错。 - barryjones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接