/// <summary>
        /// Returns the currently selected path from windows explorer.
        /// Can point into a directory, a file inside a zip file, control panel, whatever.
        /// </summary>
        /// <returns></returns>
        public static string GetTopSelectedPathFromWindowsExplorer()
        {
            IShellDispatch5 shell = new Shell();

            var windows = shell.Windows();

            var explorerHandles = new Dictionary<IntPtr, dynamic>();

            foreach (var window in windows)
            {
                explorerHandles.Add((IntPtr) window.HWND, window);
            }

            IntPtr i = GetForegroundWindow();

            while (i != IntPtr.Zero && !explorerHandles.ContainsKey(i))
            {
                i = GetWindow(i, GetWindow_Cmd.GW_HWNDNEXT);
            }

            string path = null;
            if (i != IntPtr.Zero)
            {
                var window = explorerHandles[i];
                path = ((IShellFolderViewDual2) window.Document).FocusedItem.Path;
            }
            return path;
        }
Example #2
0
        public static List<SelectedItem> GetSelectedItems(IntPtr windowHandle)
        {
            var results = new List<SelectedItem>();

            IntPtr handle = WinApi.GetForegroundWindow();

            if (handle == progmanHandle || handle == windowHandle)
            {
                Logger.Debug("Desktop listview is active", LogSource.Interop);

                // Desktop has focus
                foreach (var file in GetSelectedItemsOnDesktop())
                    results.Add(file);
            }
            else
            {
                Logger.Debug("Desktop listview was not active, expected handle for desktop {0} or for window {1}, actual handle {2}", LogSource.Interop, progmanHandle, windowHandle, handle);

                // Other window, try to determine if is a shell window
                Shell shell = new Shell();
                ShellWindows windows = (ShellWindows)shell.Windows();

                foreach (InternetExplorer window in windows)
                {
                    string processName = Path.GetFileNameWithoutExtension(window.FullName).ToLower();

                    if (processName.Equals("iexplore"))
                    {
                        if ((int)handle == window.HWND)
                        {
                            Logger.Debug("Active window was internet explorer instance {0}", LogSource.Interop, handle);
                            Logger.Debug("   Adding {0}", LogSource.Interop, window.LocationURL);

                            results.Add(new BrowserSelectedUrl() { Url = window.LocationURL });
                        }
                    }

                    else if (processName.Equals("explorer"))
                    {
                        if ((int)handle == window.HWND)
                        {
                            Logger.Debug("Active window was windows explorer instance {0}", LogSource.Interop, handle);

                            ShellFolderView view = (ShellFolderView)window.Document;
                            FolderItems items = view.SelectedItems();

                            foreach (FolderItem item in items)
                            {
                                Logger.Debug("   Adding path {0}", LogSource.Interop, item.Path);

                                results.Add(new ExplorerSelectedFile() { Location = Path.GetDirectoryName(item.Path), Filename = Path.GetFileName(item.Path) });
                            }
                        }
                    }
                }
            }

            return results;
        }