Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to close a particular instance of explorer with C#?

Tags:

c#

process

I'm looking for a way to close a Windows explorer window that's open to a certain folder. Say c:\users\bob\folder. I can close all explorers with the code below, but this is obviously not what I want to do. Is this possible?

 foreach (Process p in Process.GetProcessesByName("explorer"))
 {
    p.Kill();
 }

Thanks

like image 832
AngryTech Avatar asked Nov 19 '12 22:11

AngryTech


People also ask

What do I do if I accidentally close Windows Explorer?

Find and click Windows Explorer, and click Restart in the lower right corner. Alternatively, right-click Windows Explorer and select Restart. Alternatively, right-click Windows Explorer and select End task. Click File and Select Run new task.

How do I close all folders in Windows 10?

You can also use the following keyboard shortcuts: To open all folders at the same level as the current folder, press ALT+SHIFT+RIGHT ARROW. To close all folders at the same level as the current folder, press ALT+SHIFT+LEFT ARROW.


3 Answers

This works. It's a follow up on Jeff Roe's post.

// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);

// caption is the window title.
public void CloseWindowsExplorer(string caption)
{
    IntPtr i = User32.FindWindowByCaption(IntPtr.Zero, caption);
    if (i.Equals(IntPtr.Zero) == false)
    {
        // WM_CLOSE is 0x0010
        IntPtr result = User32.SendMessage(i, 0x0010, IntPtr.Zero, null);
    }
}
like image 76
Stephen Naughton Avatar answered Oct 18 '22 19:10

Stephen Naughton


This article that got me most of the way there: http://omegacoder.com/?p=63

I found a way using a COM library called "Microsoft Internet Controls" that looks more intended for Internet Explorer, but I gave up trying to use the process ID's and MainWindowTitle stuff since explorer.exe only uses one process for all open windows and I couldn't pin down how to get the window title text or file system location from that.

So first, add a reference to Microsoft Internet Controls from the COM tab, then:

using SHDocVw;

This little routine did the trick for me:

ShellWindows _shellWindows = new SHDocVw.ShellWindows();
string processType;

foreach (InternetExplorer ie in _shellWindows)
{
    //this parses the name of the process
    processType = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

    //this could also be used for IE windows with processType of "iexplore"
    if (processType.Equals("explorer") && ie.LocationURL.Contains(@"C:/Users/Bob"))
        {
            ie.Quit();
        }    
}

One caveat, and probably owing to the fact this library is geared toward IE, is you have to use forward slashes in your folder path... That's because the true LocationURL that comes back from the ie object is in the form file:///C:/Users/...

like image 35
Andy Raddatz Avatar answered Oct 18 '22 20:10

Andy Raddatz


I would try importing user32.dll and calling FindWindow or FindWindowByCaption, followed by a call to DestroyWindow.

Info about FindWindow is here: http://www.pinvoke.net/default.aspx/user32.findwindow

like image 26
Jeff Roe Avatar answered Oct 18 '22 20:10

Jeff Roe