private static void CloseExplorerWindows() { Shell32.Shell shell = new Shell32.Shell(); // ref: Shell.Windows method // https://msdn.microsoft.com/en-us/library/windows/desktop/bb774107(v=vs.85).aspx System.Collections.IEnumerable windows = shell.Windows() as System.Collections.IEnumerable; int count = 0; Console.WriteLine(count); if (windows != null) { // ref: ShellWindows object // https://msdn.microsoft.com/en-us/library/windows/desktop/bb773974(v=vs.85).aspx int i = 0; foreach (SHDocVw.InternetExplorer window in windows) { //Console.WriteLine(window.LocationName); object doc = window.Document; //Console.WriteLine(doc); if (doc != null && doc is Shell32.ShellFolderView) { Console.WriteLine(window.GetHashCode()); Console.WriteLine(window.LocationName); Console.WriteLine(); //Console.WriteLine(window.FullName); if (window.LocationName == "Control Panel") { Console.WriteLine("CONTROL PANEL"); //window.Quit(); } // closes the window } } Console.ReadKey(); } }
private List <string> GetSelectedItems() { IntPtr handle = GetForegroundWindow(); List <string> selected = new List <string>(); foreach (SHDocVw.InternetExplorer window in shell.Windows()) { if (window.HWND == (int)handle) { Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); foreach (Shell32.FolderItem item in items) { if (!item.IsFolder) { selected.Add(item.Path); } } } } return(selected); }
private bool IsFileOrFolderSelected() { IntPtr handle = User32Interop.GetForegroundWindow(); var shell = new Shell32.Shell(); foreach (SHDocVw.InternetExplorer window in shell.Windows()) { if (window.HWND == (int)handle) { Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); if (items.Count > 0) { return(true); } } } return(false); }
static void Main(string[] args) { IntPtr handle = GetForegroundWindow(); List <string> selected = new List <string>(); Shell32.Shell shell = new Shell32.Shell(); foreach (var window in shell.Windows()) { var testy = window.HWND; var testy2 = (int)handle; if (window.HWND == (int)handle) { Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); foreach (Shell32.FolderItem item in items) { if (args[0] == "browse") { if (item.Path[1] == ':') { String s = String.Format("/select,\"{0}\"", item.Path); shell.ShellExecute("explorer.exe", s); } else { shell.Explore(Path.GetDirectoryName(item.Path)); } } if (args[0] == "open") { shell.ShellExecute(args[1], item.Path); } } } } }
private static void Move(string dest) { IntPtr handle = GetForegroundWindow(); List <string> selected = new List <string>(); var shell = new Shell32.Shell(); foreach (SHDocVw.InternetExplorer window in shell.Windows()) { if (window.HWND == (int)handle) { Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems(); foreach (Shell32.FolderItem item in items) { selected.Add(item.Path); AccessSQL.AccessHandler.Viesti(item.Path); } } } foreach (string path in selected) { File.Move(path, dest + "\\" + Path.GetFileName(path)); } }
private List <ExplorerWindow> GetExplorerWindows(Func <SHDocVw.IWebBrowser2, List <ExplorerWindow>, bool> funcBreak) { var list = new List <ExplorerWindow>(); Shell32.Shell shell = null; SHDocVw.ShellWindows win = null; IEnumerator enumerator = null; try { shell = new Shell32.Shell(); win = shell.Windows(); enumerator = win.GetEnumerator(); SHDocVw.IWebBrowser2 web = null; while (enumerator.MoveNext()) { web = enumerator.Current as SHDocVw.IWebBrowser2; try { if (funcBreak(web, list)) { break; } } catch (Exception) { throw; } finally { if (web != null) { Marshal.ReleaseComObject(web); } } } return(list); } catch (Exception) { throw; } finally { ICustomAdapter adapter = enumerator as ICustomAdapter; if (adapter != null) { Marshal.ReleaseComObject(adapter.GetUnderlyingObject()); } if (win != null) { Marshal.ReleaseComObject(win); } if (shell != null) { Marshal.ReleaseComObject(shell); } adapter = null; enumerator = null; win = null; shell = null; // Application オブジェクトのガベージ コレクトを強制します。 GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } }