使用Visual Basic执行多个bat文件

问题描述 投票:1回答:2

我正在使用Visual Studio 2013创建的程序。该程序完成了一些工作,我几乎完成了,但是出现了最后一个问题。

在程序中,我有三个按钮。一种“强制重启”,“手动启动”和一种“强制停止”。 “强制停止”停止一堆程序,“强制重启”停止它们,然后再次启动它们。 “手动启动”启动所有程序。我将在下面使用“手动启动”作为示例。

这些按钮后面发生的事情是,它会启动一堆完成任务的蝙蝠文件。 batfile包含tasskill /f /im program.exestart "c:\program.exe“。然后出现超时并退出。

问题:

到目前为止,一切都很好。问题是,当批处理启动程序时,VB程序不会在下一个bat文件上移动。它使cmd.exe运行。甚至我也有exit。现在,如果我要手动关闭程序或cmd.exe,它将在下一个bat文件中启动。

现在基本上是这样的:VB按钮->批处理开始->批处理运行程序->批处理没有关闭AKA VB不会移动到下一个批处理。

它应该是这样的:VB按钮->批处理开始->批处理运行程序->批处理退出->下一个批处理启动->批处理运行程序->批处理退出-> ...

这是我到目前为止在VB脚本的这一部分中得到的内容:

Private Sub btnManualStart_Click(sender As Object, e As EventArgs) Handles btnManualStart.Click
If MessageBox.Show("Do you want to manually start all programs and services?", "OBS", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
        Timer2.Start()
        Dim FileNum As Integer = FreeFile()
        FileClose(FileNum)
        TextBox1.Text &= Environment.NewLine & TimeOfDay & (" Manual start made")
        Dim shell
        shell = CreateObject("wscript.shell")
        shell.run("C:\RestartApps\Scripts\Start_program1.bat", 0, True)
        shell.run("C:\RestartApps\Scripts\Start_program2.bat", 0, True)
        shell.run("C:\RestartApps\Scripts\Start_program3.bat", 0, True)
        shell.run("C:\RestartApps\Scripts\Start_program4.bat", 0, True)
        shell = Nothing
end if

希望这是可以理解的。

vb.net batch-file cmd
2个回答
2
投票

我不确切知道您的批次正在做什么,但是在我看来,您可以简单地使用Shell快捷方式:

Dim commands As String() = {
    "C:\RestartApps\Scripts\Start_program1.bat",
    "C:\RestartApps\Scripts\Start_program2.bat",
    "C:\RestartApps\Scripts\Start_program3.bat",
    "C:\RestartApps\Scripts\Start_program4.bat"
}

For Each cmd As String In commands
    Shell(cmd, AppWinStyle.Hide, False)
Next

请确保在重载(字符串,AppWinStyle,布尔值)上将第三个参数设置为FALSE。该布尔值确保将执行设置为“即发即弃”。 (与您已经传递的相同,为TRUE(将等待退出代码))。

EDIT:将AppWinStyle更改为Hide,它将以静默方式运行批处理


1
投票

您需要按如下方式改进批处理脚本中使用的start

start "" /B "c:\program.exe"

Start命令:启动程序,命令或批处理脚本(opens in a new window)。注意:

  • ""总是包含TITLE,这可以是简单的字符串,也可以是一对空引号"";
  • /B启动应用程序没有创建新窗口

另一种方法:如下使用call代替start ""

call "c:\program.exe"

0
投票

我正试图到达那里....对不起。

© www.soinside.com 2019 - 2024. All rights reserved.