有没有一种方法来关闭资源管理器中的一个特定实例用C#? [英] Is there a way to close a particular instance of explorer with C#?
问题描述
我正在寻找一种方式来关闭Windows资源管理器窗口,是开放给特定的文件夹。例如c:\users\bob\folder。我可以关闭所有的探险家与下面的代码,但是这显然不是我想做的事情。这可能吗?
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
推荐答案
这一篇文章,让我最的方式出现: http://omegacoder.com/?p = 63
This article that got me most of the way there: http://omegacoder.com/?p=63
我发现使用名为Microsoft Internet控制,看起来更预期的Internet Explorer COM库的方式,但我放弃了尝试使用进程ID和 MainWindowTitle
东西,因为Explorer.exe的只使用一个进程打开的所有窗口,我无法牵制如何从获取窗口标题文本或文件系统位置那。
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.
因此,首先,从COM选项卡添加到Microsoft Internet控件的引用,则:
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();
}
}
一个警告,而这可能是由于这样的事实:这个库向IE面向,是你必须使用着你的文件夹路径斜杠......这是因为真正的 LocationURL
从回来即
对象的格式为的file:/// C:/用户/ ...
这篇关于有没有一种方法来关闭资源管理器中的一个特定实例用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!