Close an application using VBScript

后端 未结 5 1639
南旧
南旧 2020-12-10 08:10

I was trying to open and close an application. I tried like this:

Dim App1 
Set App1 = CreateObject(\"WScript.Shell\")
App1.Run(\"firefox\")
App1.Quit
         


        
5条回答
  •  鱼传尺愫
    2020-12-10 08:51

    I observed a few ways:

    1. taskkill - Worked only if I try to kill the same process, as I run.

      Dim oShell : Set oShell = CreateObject("WScript.Shell")
      oShell.Run """C:\My_Scripts\ddl.exe"" -p1 -c"
      
      'some code
      
      oShell.Run "taskkill /f /im ddl.exe", , True
      
    2. SendKeys - Works for all app, if the window name is known.

      Dim oShell : Set oShell = CreateObject("WScript.Shell")
      filename = "C:\Some_file.txt - Notepad"
      act = oShell.AppActivate(fileName)
      oShell.SendKeys "% C"
      
    3. WMI object - 2 ways above worked good for me so I didn't try it. You can find an example here:

      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\.\root\cimv2")
      
      Set colProcessList = objWMIService.ExecQuery _
          ("Select * from Win32_Process Where Name = 'Chrome.exe'")
      
      Set oShell = CreateObject("WScript.Shell")
      For Each objProcess in colProcessList
          oShell.Run "taskkill /im chrome.exe", , True
      Next
      

提交回复
热议问题