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/...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…