扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Public Sub KillProcessFromMemorry()
创新互联建站主要从事网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务鼓楼,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
'注意进程名称的大小写
Dim procName As String = "EXCEL"
For Each proc In Process.GetProcesses
If proc.ProcessName Like procName Then
Console.WriteLine("'{0}' is running, do you want to quit?(y, n)", procName)
If Console.ReadKey.Key = ConsoleKey.Y Then
proc.Kill()
End If
End If
Next
End Sub
假设进程PID在变量runPID中:
Try
Dim deadProcess As Process = Process.GetProcessById(runPID) '获取该进程
deadProcess.Kill() '杀死进程
Catch ex As Exception
End Try
好像不难吧?
我放进了Button1的Click事件里。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error GoTo Errmessages '在做系统操作时加排错标签是个好习惯
Dim TargetName As String = "ibmdict" '存储进程名为文本型,注:进程名不加扩展名
Dim TargetKill() As Process = Process.GetProcessesByName(TargetName) '从进程名获取进程
Dim TargetPath As String '存储进程路径为文本型
If TargetKill.Length 1 Then '判断进程名的数量,如果同名进程数量在2个以上,用For循环关闭进程。
For i = 0 To TargetKill.Length - 1
TargetPath = TargetKill(i).MainModule.FileName
TargetKill(i).Kill()
Next
ElseIf TargetKill.Length = 0 Then '判断进程名的数量,没有发现进程直接弹窗。不需要的,可直接删掉该If子句
MsgBox("没有发现进程!")
Exit Sub
ElseIf TargetKill.Length = 1 Then '判断进程名的数量,如果只有一个,就不用For循环
TargetKill(0).Kill()
End If
MsgBox("已终止" TargetKill.Length "个进程") '弹窗提示已终止多少个进程
Errmessages: ‘定义排错标签
If Err.Description Nothing Then ’判断有无错误,如果有,则 ↓
MsgBox(Err.Description) '当出现错误时,弹窗提示
End If
End Sub
可根据需要自行修改,这个备注够完善了吧?不会的再Hi我。
Imports System
Imports System.Management
Module Module1
Public Sub Main()
Dim service As ManagementObject = New ManagementObject( "win32_service=""winmgmt""")
Dim options As InvokeMethodOptions = New InvokeMethodOptions
options.Timeout = New TimeSpan(0, 0, 0 , 5)
Dim outParams As ManagementBaseObject = service.InvokeMethod("StopService" , Nothing, options)
Console.WriteLine("状态:" outParams("Returnvalue"))
End Sub
End Module
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流