有没有办法用C#关闭浏览器的特定实例?

问题描述:

我正在寻找一种方法来关闭打开到某个文件夹的Windows资源管理器窗口。说c:\ users \ bob \文件夹。我可以用下面的代码关闭所有的探索者,但这显然不是我想要做的。这可能吗?有没有办法用C#关闭浏览器的特定实例?

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

感谢

+0

你打开自己这个探险家吗?如果是这样,你可以坚持进程ID。 – Blorgbeard

+0

不,我没有。谢谢。 – AngryTech

在这篇文章中让我最有方式:http://omegacoder.com/?p=63

我发现使用名为“Microsoft Internet控制”一个COM库的方式,看起来更预期对于Internet Explorer,但我放弃了尝试使用进程ID和MainWindowTitle的东西,因为explorer.exe只对所有打开的窗口使用一个进程,并且我无法确定如何从中获取窗口标题文本或文件系统位置。

因此,首先,从COM选项卡添加到Microsoft Internet控件的引用,则:

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/...

+1

嘿,这很聪明。谢谢! – AngryTech

+1

很高兴能帮到你!我喜欢你可以使用它的IE窗口以及...我在想一个经理走动恐慌按钮? ;) –

+1

我可能会迟到这次派对,但是这帮了我很大的忙,救了我这么多无用的研究时间,所以我不得不亲自谢谢你:) – Sossenbinder

我会尝试进口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(); 
    } 
} 
+0

我只看到一个进程对于Windows 7中的多个资源管理器实例,标题为空。 –