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

房东的猫 提交于 2019-12-11 07:46:19

问题


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.


回答1:


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?




回答2:


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.



回答3:


See this example: http://blogs.msdn.com/oldnewthing/archive/2004/07/20/188696.aspx 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.



来源:https://stackoverflow.com/questions/1084418/how-do-i-keep-windows-explorer-from-interfering-with-deleting-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!