有没有办法用C#关闭特定的资源管理器实例?

我正在寻找一种方法来关闭一个对某个文件夹打开的Windows资源管理器窗口。 说c:\ users \ bob \文件夹。 我可以用下面的代码关闭所有的探险家,但这显然不是我想要做的。 这可能吗?

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

谢谢

这篇文章让我大部分都在那里: http : //omegacoder.com/?p = 63

我找到了一种方法,使用名为“Microsoft Internet Controls”的COM库看起来更适合Internet Explorer,但我放弃了尝试使用进程ID和MainWindowTitle东西,因为explorer.exe只对所有打开的窗口使用一个进程而且我不能要确定如何从中获取窗口标题文本或文件系统位置。

首先,从COM选项卡添加对Microsoft Internet Controls的引用,然后:

 using SHDocVw; 

这个小程序对我有用:

 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(); } } 

一个警告,可能是因为这个库面向IE,你必须在你的文件夹路径中使用正斜杠…这是因为从ie对象返回的真正的LocationURL在表单file:///C:/Users/...

我会尝试导入user32.dll并调用FindWindow或FindWindowByCaption,然后调用DestroyWindow。

有关FindWindow的信息,请访问: http ://www.pinvoke.net/default.aspx/user32.findwindow

你可以尝试这样的事情:

 foreach (Process p in Process.GetProcessesByName("explorer")) { if (p.MainWindowTitle.ToLower().Contains(@"c:\users\bob\folder")) p.Kill(); } 
 foreach (Process p in Process.GetProcessesByName("explorer")) { if (p.MainWindowTitle.Contains("YourFolderName")) { p.Kill(); } }