gpt4 book ai didi

vb.net - 从 Windows 窗体关闭命令窗口

转载 作者:太空宇宙 更新时间:2023-11-03 22:55:48 26 4
gpt4 key购买 nike

我有一个简单的 Node.js 程序,它通过 winform 中的按钮单击执行,工作正常,但我需要终止 Node.js 程序并在另一个按钮单击时关闭命令提示符。我怎样才能实现这一点?

干杯吉夫

最佳答案

如果您启动进程,您就会拥有一个 process 类型的对象。该对象支持kill,它将立即结束进程。

更新代码

Public Class Form1

Dim MyProcess As Process

Private Sub btnStartPrcoess_Click(sender As Object, e As EventArgs) Handles btnStartPrcoess.Click

If MyProcess Is Nothing Then
MyProcess = Process.Start("cmd.exe", "arguments")
End If

End Sub

Private Sub btnKillProcess_Click(sender As Object, e As EventArgs) Handles btnKillProcess.Click

If MyProcess IsNot Nothing Then
MyProcess.Kill()
MyProcess.Close()
MyProcess = Nothing
End If

End Sub
End Class

如果您需要从不同的方法结束进程,当然需要在类级别声明进程变量。甚至Process.Start()也有一个process类型的返回值。因此无需搜索该过程 - 您已经知道了!

更新

做这样的事情或多或少是无稽之谈:

MyProcess = Process.Start("cmd.exe", "/k notepad.exe")

因为它只是启动 cmd.exe,然后启动 notepad.exe。当然,Process 现在“指向”cmd.exe,而不是 notepad.exe。如果您想让记事本运行,那么直接启动它显然是最好的解决方案:

MyProcess = Process.Start("notepad.exe", "arguments for notepad, if needed")

关于vb.net - 从 Windows 窗体关闭命令窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14065212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com