c#如何获取窗口资源管理器鼠标光标下的目录路径

时间:2013-06-16 06:20:24

标签: c# .net explorer

我想获取鼠标光标下的窗口浏览器目录路径。 我试过AutomationElement,但它只检测我不想要的名称或进程ID等。一世 也尝试了SHDocVw.ShellWindows似乎有效,但我无法确定选择哪一个。

这是我试过的一个来源

string GetDropPath(AutomationElement element){
            string dir = element.Current.Name;

            if (dir.Split('.').Length >= 2) return null;

            string desktop = CommonDir["Desktop"];
            if (Directory.Exists(desktop + "\\" + dir))
            {
                dir = desktop + "\\" + dir;
            }
            else
            {
                if (CommonDir.ContainsKey(dir))
                {
                    dir = CommonDir[dir];
                }
                else if (!Directory.Exists(dir))
                {


                    foreach (InternetExplorer item in new SHDocVw.ShellWindows())
                    {
                        //uint ppid = 0;
                        //GetWindowThreadProcessId(item.HWND, out ppid);
                        if (item.LocationURL.ToString().StartsWith("file:///"))
                        {
                            List<int> children = GetAllChildrenWindowHandles(item.HWND, 100);
                            if (item.HWND == element.Current.NativeWindowHandle || children.Contains(element.Current.NativeWindowHandle))
                            {
                                string pth = item.LocationURL.Replace("file:///", "");
                                if (Directory.Exists(pth + "\\" + dir))
                                {
                                    dir = pth + "\\" + dir;
                                }
                                else
                                {
                                    dir = pth;
                                }
                                break;
                            }
                        }
                    }

                }
            }
            if (Directory.Exists(dir))
            {
                return dir;
            }
            else
            {
                return null;
            }
        }

0 个答案:

没有答案
相关问题