我试图弄清楚如何杀死两个进程,同时我设法让它在打开时工作,但另一个不会关闭。
Sub block()
For Each item As Process In Process.GetProcesses
If item.ProcessName = "taskmgr" And item.ProcessName = "cmd" Then
item.Kill()
End If
Next
End Sub
答案 0 :(得分:2)
正如@Noodles和@Zaggler所说,你的逻辑在这一行上是错误的;
If item.ProcessName = "taskmgr" And item.ProcessName = "cmd" Then
这一行基本上询问进程名称是否为" taskmgr"如果相同的进程名称为" cmd"。因为这两个字符串不是相同的" taskmgr" / =" cmd"这个if子句永远不会成立。我建议你做这样的事情;
Sub block()
For Each item As Process In Process.GetProcesses
If item.ProcessName = "taskmgr" Then
item.Kill()
ElseIf item.ProcessName = "cmd" Then
item.Kill()
End If
Next
End Sub
或者,如果您计划关闭许多流程,也可以选择;
'declare at form loading or elsewhere
Dim proclist as new list (of string)
proclist.add("taskmgr")
proclist.add("cmd")
proclist.add("...")
Sub block()
For Each item As Process In Process.GetProcesses
If proclist.contains(item.ProcessName) Then
item.Kill()
End If
Next
End Sub
答案 1 :(得分:1)
在vbscript中尝试使用此解决方案:
Option Explicit
Dim Ws,fso,MainArray,LogFile,i,OutPut,count
Set Ws = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
MainArray = Array("taskmgr.exe","cmd.exe")
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "log"
count = 0
If fso.FileExists(LogFile) Then fso.DeleteFile LogFile
Set OutPut = fso.OpenTextFile(LogFile,2,True)
For i = LBound(MainArray) To UBound(MainArray)
Call Kill(MainArray(i))
Next
OutPut.WriteLine String(50,"*")
OutPut.WriteLine count & " Process were killed !"
OutPut.WriteLine String(50,"*")
If fso.FileExists(LogFile) Then
ws.run DblQuote(LogFile) 'To show the LogFile
End if
'******************************************
Sub Kill(MyProcess)
Dim colItems,objItem
Set colItems = GetObject("winmgmts:").ExecQuery("Select * from Win32_Process " _
& "Where Name like '%"& MyProcess &"%' AND NOT commandline like '%" & wsh.scriptname & "%'",,48)
For Each objItem in colItems
count= count + 1
OutPut.WriteLine Mid(objItem.CommandLine,InStr(objItem.CommandLine,""" """) + 2)
objItem.Terminate(0)
Next
End Sub
'***********************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***********************************************