我正在尝试使用 VBScript 制作一个简单的程序,每次打开特定文件夹时都会关闭该文件夹,从而拒绝对该文件夹的访问。我已经成功地在许多文件夹中使用了此代码,但由于某种原因它不适用于C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
.
Do
WindowTitle = "FOLDERNAME"
Set shell = CreateObject("WScript.Shell")
success = shell.AppActivate(WindowTitle)
If success Then shell.SendKeys "%{F4}"
Loop
有什么方法可以拒绝使用 .vbs 文件访问该特定文件夹吗?
你可以这样尝试:
myfolder = "C:\temp"
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If w.document.folder.self.Path = myfolder Then w.Quit
Next
这是关闭临时文件夹的完整示例:
Option Explicit
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF &_
CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Dim MyFolder,ws
Set ws = CreateObject("wscript.shell")
Myfolder = ws.ExpandEnvironmentStrings("%temp%")
Do
Call CloseThis(MyFolder)
wscript.sleep 1000
Loop
End If
'*********************************************************************************************
Sub CloseThis(Folder)
Dim sh,w
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If w.document.folder.self.Path = Folder Then w.Quit
Next
End Sub
'*********************************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'*********************************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'*********************************************************************************************
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)