VB如何结束使用按钮运行的batchfile / cmd命令?

时间:2013-02-22 04:36:34

标签: vb.net cmd

这是我正在使用的代码:

Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Process.Start("C:\batchfile.bat")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub

我想知道如何让Button1_Click杀掉“batchfile.bat?”批处理文件正在打开一个CMD提示符,如果这对任何人都有帮助。

1 个答案:

答案 0 :(得分:2)

您需要在表单的字段中启动Process返回的Process.Start()实例。

然后,您可以调用Kill()方法。

例如:

Public Class Form1
    Private mySubTask As Process

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        mySubTask = Process.Start("C:\batchfile.bat")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mySubTask.Kill()
    End Sub

如果用户点击两次启动,您需要弄清楚要做什么。