繁体   English   中英

如何获取“文件资源管理器”打开的窗口的目录?

[英]How can i get the directories of File Explorer opened windows?

例如,在任务栏上,我右键单击“文件资源管理器”>“文件资源管理器”>“打开”,例如,现在我在C:\\

现在,我想以某种方式使用csharp代码获取此文件浏览器打开的窗口目录(在这种情况下为C:\\),如果我要打开文件浏览器的新窗口并转到c:\\ temp,我现在将再次运行该程序将具有两个路径的两个字符串的数组或列表:窗口1:C:\\窗口2:C:\\ Temp

到目前为止我尝试过的是:

在form1的顶部:

[DllImport("user32.dll", SetLastError = true)]
 static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

然后在构造函数中调用GetDirs:

public Form1()
        {
            InitializeComponent();

            GetDirs();
        }

然后方法GetDirs:

private void GetDirs()
        {
            IntPtr MyHwnd = FindWindow(null, "Directory");
            var t = Type.GetTypeFromProgID("Shell.Application");
            dynamic o = Activator.CreateInstance(t);
            try
            {
                var ws = o.Windows();
                for (int i = 0; i < ws.Count; i++)
                {
                    var ie = ws.Item(i);
                    if (ie == null || ie.hwnd != (long)MyHwnd) continue;
                    var path = System.IO.Path.GetFileName((string)ie.FullName);
                    if (path.ToLower() == "explorer.exe")
                    {
                        var explorepath = ie.document.focuseditem.path;
                    }
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(o);
            } 
        }

但这从来没有达到终点:

var explorepath = ie.document.focuseditem.path;

而且我不确定explorpath是什么类型的var。

要在所有打开的资源管理器窗口中获得重点项目的目录名,请执行以下操作:

        var t = Type.GetTypeFromProgID("Shell.Application");
        dynamic o = Activator.CreateInstance(t);
        try
        {
            var ws = o.Windows();
            for (int i = 0; i < ws.Count; i++)
            {
                var ie = ws.Item(i);
                if (ie == null) continue;
                var path = System.IO.Path.GetFileName((string)ie.FullName);
                if (path.ToLower() == "explorer.exe")
                {
                    var explorepath = System.IO.Path.GetDirectoryName(ie.document.focuseditem.path);
                }
            }
        }
        finally
        {
            Marshal.FinalReleaseComObject(o);
        }

我不确定您要使用FindWindow实现什么,这基本上是我删除的。

您是否尝试过使用Microsoft Internet Controls SHDocVw 例如,此代码清单可以显示当前打开的目录的路径。

SHDocVw.ShellWindows shellWindows = null;
try
{
    shellWindows = new SHDocVw.ShellWindows();
    foreach (Set_folder_view_2.WinAPI._IServiceProvider serviceProvider in shellWindows)
    {
        SHDocVw.InternetExplorer ie = (SHDocVw.InternetExplorer)serviceProvider;

        if (Path.GetFileNameWithoutExtension(ie.FullName).Equals("explorer", StringComparison.OrdinalIgnoreCase))
        {
            Console.WriteLine(ie.LocationURL);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}
finally
{
    if (shellWindows != null)
    {
        Marshal.ReleaseComObject(shellWindows);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM