FSO DeleteFolder()方法不起作用 [英] FSO DeleteFolder() method not working

查看:281
本文介绍了FSO DeleteFolder()方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTA中有一个程序,所有辅助文件都在同一个文件夹中,该文件夹是AppData文件夹的子文件夹。我创建了一个卸载程序HTA,只需使用FSO DeleteFolder()方法删除文件夹即可卸载程序。我使用HtaEdit将其转换为可执行文件。 (如果您不知道该程序,那就没关系了)。可执行文件的作用是在临时文件夹(以及辅助文件)中创建HTA并运行它。问题在于,当它执行 DeleteFolder()方法时,会出现一条错误消息,提示拒绝访问。我认为这不是管理员问题,因为它位于当前用户的AppData文件夹中。当我尝试以这种方式删除另一个文件夹时,它工作正常。我认为通常在删除包含正在运行的HTA文件的文件夹时会遇到问题,但是该HTA文件不在我要删除的文件夹中,而是在一个临时文件夹中。但是,它被我要删除的文件夹中的可执行文件调用。

I have a program in HTA and all the auxiliary files are in the same folder, a subfolder of the AppData folder. I created an uninstaller HTA which uninstalls the program by simply removing the folder with FSO DeleteFolder() method. I converted it to an executable with HtaEdit. (If you don't know this program, it doesn't matter). What the executable does is that it creates an HTA in a temporary folder (along with the auxiliary files) and runs it. The problem is that when it does the DeleteFolder() method, an error message comes up saying "denied access". I don't think that it's an administrator problem since it's in the current user's AppData folder. When I try deleting another folder that way, it works just fine. I think that there are usually problems deleting the folder containing an HTA file which is being run, but the HTA file isn't in the folder I'm trying to delete but in a temporary folder. However, it's been called by an executable in the folder I'm trying to delete.

我正在使用VBScript,但是如果使用JavaScript,它也会执行相同的操作。

I'm using VBScript, but it does the same thing if I use JavaScript.

推荐答案

只要有打开文件夹的文件夹,您就无法从同一文件夹中删除该文件夹或里面的东西。例如,以下代码通常将删除VBScript的父文件夹:

You can't delete a folder from within the same folder as long as there's still something holding an open handle to the folder or something inside it. For example, the following code will usually delete the parent folder of a VBScript:

Set fso = CreateObject("Scripting.FileSystemObject")
dir = fso.GetParentFolderName(WScript.ScriptFullName)
fso.DeleteFolder(dir)

,或者,如果是HTA(没有 WScript 对象):

or, in case of an HTA (which doesn't have a WScript object):

Set fso = CreateObject("Scripting.FileSystemObject")
htaPath = Replace(oHTA.CommandLine, """", "")  '<HTA:APPLICATION ID="oHTA" ...>
dir = fso.GetParentFolderName(htaPath)
fso.DeleteFolder(dir)

这些工作是因为脚本解释器在脚本启动时将整个脚本读入内存,因此不会保留文件的打开句柄。

These work, because the script interpreter reads the entire script into memory when the script is launched, so no open handle to the file remains.

但是,如果文件夹是脚本进程的当前工作目录,则文件夹的删除将失败,并显示权限被拒绝错误,因为在这种情况下,该文件夹仍然有打开的句柄。例如,如果文件夹在资源管理器或命令提示符中打开,则同样适用。

However, the deletion of the folder will fail with a "permission denied" error if the folder is the current working directory of the script process, because in that situation there still is an open handle to the folder. The same applies if, for instance, the folder is open in Explorer or a command prompt.

您可以使用 handle.exe 流程浏览器

这篇关于FSO DeleteFolder()方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆