我正在寻找一种方法来关闭对某个文件夹打开的Windows资源管理器窗口.说c:\ users\bob \文件夹.我可以用下面的代码关闭所有的探险家,但这显然不是我想做的.这可能吗?
foreach (Process p in Process.GetProcessesByName("explorer"))
{
p.Kill();
}
Run Code Online (Sandbox Code Playgroud)
谢谢
And*_*atz 18
这篇文章让我大部分都在那里:http://omegacoder.com/?p = 63
我找到了一种方法,使用名为"Microsoft Internet Controls"的COM库看起来更适用于Internet Explorer,但我放弃了尝试使用进程ID和MainWindowTitle东西,因为explorer.exe只对所有打开的窗口使用一个进程,我无法' t如何从中获取窗口标题文本或文件系统位置.
首先,从COM选项卡添加对Microsoft Internet Controls的引用,然后:
using SHDocVw;
Run Code Online (Sandbox Code Playgroud)
这个小例程对我有用:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
一个警告,可能是因为这个库面向IE,你必须在你的文件夹路径中使用正斜杠...那是因为LocationURL从ie对象返回的真实是在形式file:///C:/Users/...