[英]Is it possible to make a VB program interact with a command prompt?
我正在学习Java,但感到很烦,您必须使用命令提示符来编译和运行程序...所以我在想:是否有一种方法可以在Visual Basic中制作一个可以打开命令提示符的程序并输入代码?
您可以使用Process类。
Using p As New Process
p.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "CMD.exe")
p.StartInfo.Arguments = String.Format("/C ""{0}"" ", "Dir /B /S /A-D ""*""")
p.StartInfo.WorkingDirectory = Environment.SystemDirectory
p.Start()
p.WaitForExit()
End Using
但是,无需调用CMD,只需直接调用Java编译器即可,就像上面的示例一样。 或者,你也可以考虑directlly开发的Java程序,并使用Java编译器的接口有关。
使用此代码来运行您需要在命令提示符下运行的一批命令。
Dim commands As Object() = New Object() {"echo Command One", "echo Command Two", "..."}
Dim startInfo As ProcessStartInfo = New ProcessStartInfo(Path.Combine(Environment.SystemDirectory, "cmd.exe"))
startInfo.WindowStyle = ProcessWindowStyle.Normal
startInfo.Arguments = "/c """
For Each obj As Object In commands
startInfo.Arguments += obj.ToString + " & "
Next
startInfo.Arguments = startInfo.Arguments.Substring(0, startInfo.Arguments.LastIndexOf(" & "))
startInfo.Arguments += """"
Process.Start(startInfo)
或者只是使用此代码在命令提示符下更简洁地运行命令。
Shell("cmd /c ""echo Command One & Command Two & ...""")
简而言之,是的。 您可以仅使用命令行提示符。
Sub Foo()
Dim cmd_Command As String = "DIR %USERPROFILE%/Desktop/*.* > %USERPROFILE%/Files.log"
CreateObject("WScript.Shell").Exec("CMD /C " & cmd_Command)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.