有没有办法用 C# 关闭特定的资源管理器实例? [英] Is there a way to close a particular instance of explorer with C#?

查看:29
本文介绍了有没有办法用 C# 关闭特定的资源管理器实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to close a Windows explorer window that's open to a certain folder. Say c:usersobfolder. 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();
 }

谢谢

推荐答案

这篇文章让我大有收获:http://omegacoder.com/?p=63

This article that got me most of the way there: http://omegacoder.com/?p=63

我找到了一种使用名为Microsoft Internet Controls"的 COM 库的方法,它看起来更适合 Internet Explorer,但我放弃了尝试使用进程 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 Controls 的引用,然后:

So first, add a reference to Microsoft Internet Controls from the COM tab, then:

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 的,你必须在文件夹路径中使用正斜杠...那是因为真正的 LocationURLie 对象的形式为 file:///C:/Users/...

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

这篇关于有没有办法用 C# 关闭特定的资源管理器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆