Forcing Explorer to close open windows in C#

  • Thread starter Thread starter Paul Steele
  • Start date Start date
P

Paul Steele

Is there any way to check for open Explorer windows and if any are found,
tell Windows Explorer to close them. I don't want a brute force method of
killing Explorer and letting it restart. I'd prefer something a little less
drastic. I suspect what I'm going to need is some API call such as
SendMessage. Anybody have any suggestions?
 
Paul Steele said:
Is there any way to check for open Explorer windows and if any are found,
tell Windows Explorer to close them. I don't want a brute force method of
killing Explorer and letting it restart. I'd prefer something a little
less drastic. I suspect what I'm going to need is some API call such as
SendMessage. Anybody have any suggestions?

I found half the answer to my original question. By using a combination of
the Windows API calls FindWindow and SendMessage, it's quite simple to close
an open Exporer window:

int iHandle=Win32.FindWindow("ExploreWClass" ,windowName);
int j=Win32.SendMessage(iHandle, Win32.WM_SYSCOMMAND, Win32.SC_CLOSE,
0);

The part I haven't figured out is how to get the name of any open windows in
Explorer. Having just the class name is not enough to get the window
handle. Another API call I suspect...
 
Paul Steele said:
I found half the answer to my original question. By using a combination of
the Windows API calls FindWindow and SendMessage, it's quite simple to
close an open Exporer window:

int iHandle=Win32.FindWindow("ExploreWClass" ,windowName);
int j=Win32.SendMessage(iHandle, Win32.WM_SYSCOMMAND, Win32.SC_CLOSE,
0);

The part I haven't figured out is how to get the name of any open windows
in Explorer. Having just the class name is not enough to get the window
handle. Another API call I suspect...

This code does what I want:

int wHandle;
while ((wHandle = Win32.FindWindow("ExploreWClass", null)) > 0)
{
Win32.SendMessage(wHandle, Win32.WM_SYSCOMMAND, Win32.SC_CLOSE, 0);
}
 
Back
Top