Public?Sub?KillProcessFromMemorry()
創(chuàng)新互聯(lián)建站主要從事網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)鼓樓,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):028-86922220
'注意進(jìn)程名稱(chēng)的大小寫(xiě)
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
假設(shè)進(jìn)程PID在變量runPID中:
Try
Dim deadProcess As Process = Process.GetProcessById(runPID) '獲取該進(jìn)程
deadProcess.Kill() '殺死進(jìn)程
Catch ex As Exception
End Try
好像不難吧?
我放進(jìn)了Button1的Click事件里。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error GoTo Errmessages '在做系統(tǒng)操作時(shí)加排錯(cuò)標(biāo)簽是個(gè)好習(xí)慣
Dim TargetName As String = "ibmdict" '存儲(chǔ)進(jìn)程名為文本型,注:進(jìn)程名不加擴(kuò)展名
Dim TargetKill() As Process = Process.GetProcessesByName(TargetName) '從進(jìn)程名獲取進(jìn)程
Dim TargetPath As String '存儲(chǔ)進(jìn)程路徑為文本型
If TargetKill.Length 1 Then '判斷進(jìn)程名的數(shù)量,如果同名進(jìn)程數(shù)量在2個(gè)以上,用For循環(huán)關(guān)閉進(jìn)程。
For i = 0 To TargetKill.Length - 1
TargetPath = TargetKill(i).MainModule.FileName
TargetKill(i).Kill()
Next
ElseIf TargetKill.Length = 0 Then '判斷進(jìn)程名的數(shù)量,沒(méi)有發(fā)現(xiàn)進(jìn)程直接彈窗。不需要的,可直接刪掉該If子句
MsgBox("沒(méi)有發(fā)現(xiàn)進(jìn)程!")
Exit Sub
ElseIf TargetKill.Length = 1 Then '判斷進(jìn)程名的數(shù)量,如果只有一個(gè),就不用For循環(huán)
TargetKill(0).Kill()
End If
MsgBox("已終止" TargetKill.Length "個(gè)進(jìn)程") '彈窗提示已終止多少個(gè)進(jìn)程
Errmessages: ‘定義排錯(cuò)標(biāo)簽
If Err.Description Nothing Then ’判斷有無(wú)錯(cuò)誤,如果有,則 ↓
MsgBox(Err.Description) '當(dāng)出現(xiàn)錯(cuò)誤時(shí),彈窗提示
End If
End Sub
可根據(jù)需要自行修改,這個(gè)備注夠完善了吧?不會(huì)的再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("狀態(tài):" outParams("Returnvalue"))
End Sub
End Module