Vb.net 可以让VB程序与命令提示符交互吗?

Vb.net 可以让VB程序与命令提示符交互吗?,vb.net,process,automation,batch-processing,javac,Vb.net,Process,Automation,Batch Processing,Javac,我正在学习Java,我发现你不得不使用命令提示符来编译和运行你的程序很烦人。。。所以我在想:有没有一种方法可以在VisualBasic中创建一个程序,打开命令提示符并键入代码 简而言之,是的。您只需shell一个命令提示符 Sub Foo() Dim cmd_Command As String = "DIR %USERPROFILE%/Desktop/*.* > %USERPROFILE%/Files.log" CreateObject("WScript.Shell")

我正在学习Java,我发现你不得不使用命令提示符来编译和运行你的程序很烦人。。。所以我在想:有没有一种方法可以在VisualBasic中创建一个程序,打开命令提示符并键入代码

简而言之,是的。您只需shell一个命令提示符

Sub Foo()

    Dim cmd_Command As String = "DIR %USERPROFILE%/Desktop/*.* > %USERPROFILE%/Files.log"

    CreateObject("WScript.Shell").Exec("CMD /C " & cmd_Command)

End Sub
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 & ...""")
您可以使用

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编译器,方法与上面的示例相同。也可以考虑在<>强> java < /St>中直接开发程序,使用java编译器的接口相关。< /P> < P>使用此代码运行命令提示符下运行的命令。
Sub Foo()

    Dim cmd_Command As String = "DIR %USERPROFILE%/Desktop/*.* > %USERPROFILE%/Files.log"

    CreateObject("WScript.Shell").Exec("CMD /C " & cmd_Command)

End Sub
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
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 & ...""")

是的,我想如果不是这样的话,用法会少很多。我想你应该把你的问题编辑得更具体一些。或者。。您可以使用JavaIDE(集成开发环境)。VB开发是在Visual Studio IDE中完成的,该IDE处理所有“编译并运行”的内容,而无需使用命令行。我看到您的第一次编辑添加了使用Java的内容,但您的问题标题仍然是指VB,您的标记是指VBA和VB.Net。我建议你把你的问题重新组织成“我怎样才能自动编译和运行Java程序?”记事本和记事本++哦,那些日子。当然,只有一个IDE在那里…我使用了该代码,cmd打开和关闭非常快。这段代码避免了错误的做法。1.创建COM对象实例时,应在使用FinalEleaseComObject方法完成工作后释放它,并将其设置为Nothing(C#中的nul)。2.没有必要考虑COM互操作作为解决方案,因为.NETFramework类库公开托管代码来做同样的事情。我知道与COM in .NET的交易;问题只是问“是否有办法……”泛型问题=泛型答案,这并不是作为完成的代码推送的。