vb.net 如何使用 Visual Basic 代码运行命令提示符命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16152531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a Command Prompt command with Visual Basic code?
提问by Fellah
I'm trying to code something in Visual Basic, more specifically Visual Studio 2010. I want, with a button click, my program to execute a command. Is that possible?
我正在尝试在 Visual Basic 中编写一些代码,更具体地说是 Visual Studio 2010。我希望通过单击按钮,我的程序来执行命令。那可能吗?
回答by ElektroStudios
Here is an example:
下面是一个例子:
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
And here is a extended function: (Notice the comment-lines using CMD commands.)
这是一个扩展功能:(注意使用 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
回答by Wally Modz
Or, you could do it the really simple way.
或者,你可以用非常简单的方法来做。
Dim OpenCMD
OpenCMD = CreateObject("wscript.shell")
OpenCMD.run("Command Goes Here")
回答by Reed Copsey
Yes. You can use Process.Start
to launch an executable, including a console application.
是的。您可以Process.Start
用来启动可执行文件,包括控制台应用程序。
If you need to read the output from the application, you may need to read from it's StandardOutputstream in order to get anything printed from the application you launch.
如果您需要从应用程序读取输出,您可能需要从它的StandardOutput流中读取,以便从您启动的应用程序打印任何内容。
回答by barryjones
You need to use CreateProcess [ http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx]
您需要使用 CreateProcess [ http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx]
For ex:
例如:
LPTSTR szCmdline[] = _tcsdup(TEXT("\"C:\Program Files\MyApp\" -L -S")); CreateProcess(NULL, szCmdline, /.../);
LPTSTR szCmdline[] = _tcsdup(TEXT("\"C:\Program Files\MyApp\" -L -S")); CreateProcess(NULL, szCmdline, / .../);