简体   繁体   中英

How do I keep Windows Explorer from interfering with deleting a folder?

I've got a routine that deletes a folder and everything in it. After deleting all the files, the last thing it does is:

if not Windows.RemoveDirectory(pname) then
  raise EInOutError.Create(SysErrorMessage(GetLastError));

Unfortunately, I tend to get an error from this if I have an open window in Windows Explorer displaying the folder. The error says the folder is not empty, which is not true. Is there any way to override this, perhaps forcing the window to close?

In case it makes a difference, I'm on Vista Home Premium 64.

Actually, it's even more general than this. You can never delete the current directory of ANY program, not just Explorer.

You could write something that hunted down explorer windows pointing at the directory of interest but what about other programs?

The following code shows the general approach for closing windows. This example is for Internet Explorer; you'll have to tweak it a bit for Windows Explorer..

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.

See this example:https://devblogs.microsoft.com/oldnewthing/20040720-00/?p=38393 . And here is the same code in 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=

You can enumerate all windows by using this example and find the Explorer's window, which is open at your folder. Then you can close it by sending WM_CLOSE message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM