我有一个删除文件夹及其中所有内容的例程。删除所有文件后,最后要做的是:
if not Windows.RemoveDirectory(pname) then
raise EInOutError.Create(SysErrorMessage(GetLastError));
[不幸的是,如果我在Windows资源管理器中有一个打开的窗口显示该文件夹,那么我往往会因此而出错。该错误表明文件夹不为空,这是不正确的。有什么方法可以替代此方法,也许会迫使窗口关闭?
如果有所作为,我使用的是Vista Home Premium 64。
实际上,它比这更笼统。您永远不能删除ANY程序的当前目录,而不仅仅是删除Explorer。
您可以编写一些内容,使其指向目标目录,但在浏览器窗口中却不起作用,但是其他程序呢?
以下代码显示了关闭窗口的一般方法。这个例子是针对Internet Explorer的。您必须对Windows资源管理器进行一些调整。]
program Sample;
function CloseIEs(Wnd : HWnd; Form : TForm1) : Boolean; export; stdcall;
var
sCap : array [0..255] of char;
begin
GetWindowText (Wnd, sCap, sizeof(sCap));
if pos ('Microsoft Internet Explorer', sCap) > 0 then
begin
PostMessage (Wnd, WM_CLOSE, 0, 0);
end
else
begin
// check by class name!
GetClassName (Wnd, sCap, sizeof(sCap));
if sCap = 'IEFrame' then
PostMessage (Wnd, WM_CLOSE, 0, 0);
end;
CloseIEs := true; { next window, please }
end;
begin
// close all hidden instances
EnumWindows(@CloseIEs, 0);
end.
参见此示例:https://devblogs.microsoft.com/oldnewthing/20040720-00/?p=38393。这是Delphi中的相同代码:http://translate.google.com/translate?prev=hp&hl=ru&js=n&u=http://transl-gunsmoker.blogspot.com/2009/05/blog-post_7575.html&sl=ru&tl=en&history_state0=
您可以通过使用此示例来枚举所有窗口,并找到在您的文件夹中打开的资源管理器窗口。然后,您可以通过发送WM_CLOSE消息将其关闭。