简体   繁体   English

是否可以使VB程序与命令提示符交互?

[英]Is it possible to make a VB program interact with a command prompt?

我正在学习Java,但感到很烦,您必须使用命令提示符来编译和运行程序...所以我在想:是否有一种方法可以在Visual Basic中制作一个可以打开命令提示符的程序并输入代码?

You can use the Process class. 您可以使用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

But there is no need to call the CMD, just call the Java compiler directlly, in the same way as the example above. 但是,无需调用CMD,只需直接调用Java编译器即可,就像上面的示例一样。 Or also you can consider to develop your program directlly in Java , and use the Java compiler's interface related. 或者,你也可以考虑directlly开发的Java程序,并使用Java编译器的接口有关。

Use this code to run the batch of commands you need to run in command prompt. 使用此代码来运行您需要在命令提示符下运行的一批命令。

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)

Or just use this code to run the commands more concisely in the command prompt. 或者只是使用此代码在命令提示符下更简洁地运行命令。

Shell("cmd /c ""echo Command One & Command Two & ...""")

In short, yes. 简而言之,是的。 You can just shell a command prompt. 您可以仅使用命令行提示符。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM