static void Main(string[] args) { var shellWindows = new ShellWindows(); foreach (IWebBrowser2 win in shellWindows) { IServiceProvider sp = win as IServiceProvider; object sb; sp.QueryService(SID_STopLevelBrowser, typeof(IShellBrowser).GUID, out sb); IShellBrowser shellBrowser = (IShellBrowser)sb; object sv; shellBrowser.QueryActiveShellView(out sv); Console.WriteLine(win.LocationURL + " " + win.LocationName); IFolderView fv = sv as IFolderView; if (fv != null) { // only folder implementation support this (IE windows do not for example) object pf; fv.GetFolder(typeof(IPersistFolder2).GUID, out pf); IPersistFolder2 persistFolder = (IPersistFolder2)pf; // get folder class, for example // CLSID_ShellFSFolder for standard explorer folders Guid clsid; persistFolder.GetClassID(out clsid); Console.WriteLine(" clsid:" + clsid); // get current folder pidl IntPtr pidl; persistFolder.GetCurFolder(out pidl); // TODO: do something with pidl Marshal.FreeCoTaskMem(pidl); // free pidl's allocated memory } } }
private static IDLWrapper GetShellPath(IFolderView folderView) { if (folderView == null) { return(null); } IPersistFolder2 ppv = null; try { Guid riid = ExplorerGUIDs.IID_IPersistFolder2; if (folderView.GetFolder(ref riid, out ppv) == 0) { IntPtr ptr; ppv.GetCurFolder(out ptr); return(new IDLWrapper(ptr)); } } catch { } finally { if (ppv != null) { Marshal.ReleaseComObject(ppv); } } return(new IDLWrapper(IntPtr.Zero)); }
public static IntPtr ShellGetPath(IShellBrowser shellBrowser) { IShellView ppshv = null; IPersistFolder2 ppv = null; try { if (shellBrowser.QueryActiveShellView(out ppshv) == 0) { Guid riid = ExplorerGUIDs.IID_IPersistFolder2; IFolderView view2 = (IFolderView)ppshv; if (view2.GetFolder(ref riid, out ppv) == 0) { IntPtr ptr; ppv.GetCurFolder(out ptr); return(ptr); } } } catch { } finally { if (ppshv != null) { Marshal.ReleaseComObject(ppshv); } if (ppv != null) { Marshal.ReleaseComObject(ppv); } } return(IntPtr.Zero); }
public void ShellTest() { var shellWindows = new ShellWindows(); foreach (IWebBrowser2 win in shellWindows) { IServiceProvider sp = win as IServiceProvider; object sb; sp.QueryService(SID_STopLevelBrowser, typeof(IShellBrowser).GUID, out sb); IShellBrowser shellBrowser = (IShellBrowser)sb; object sv; shellBrowser.QueryActiveShellView(out sv); Console.WriteLine(win.LocationURL + " " + win.LocationName); IFolderView fv = sv as IFolderView; if (fv != null) { // only folder implementation support this (IE windows do not for example) object pf; fv.GetFolder(typeof(IPersistFolder2).GUID, out pf); IPersistFolder2 persistFolder = (IPersistFolder2)pf; // get folder class, for example // CLSID_ShellFSFolder for standard explorer folders Guid clsid; persistFolder.GetClassID(out clsid); Console.WriteLine(" clsid:" + clsid); int pitem; Timer timer = new Timer(new TimerCallback(stat => { //if (0 == fv.GetFocusedItem(out pitem)) //{ // Console.WriteLine(pitem); //} if (0 == fv.ItemCount(0x00000001, out pitem)) { Console.WriteLine(pitem); } })); timer.Change(200, 0); // get current folder pidl IntPtr pidl; persistFolder.GetCurFolder(out pidl); // TODO: do something with pidl Marshal.FreeCoTaskMem(pidl); // free pidl's allocated memory } } }
private static IDLWrapper GetShellPath(IFolderView folderView) { if(folderView == null) return null; IPersistFolder2 ppv = null; try { Guid riid = ExplorerGUIDs.IID_IPersistFolder2; if(folderView.GetFolder(ref riid, out ppv) == 0) { IntPtr ptr; ppv.GetCurFolder(out ptr); return new IDLWrapper(ptr); } } catch { } finally { if(ppv != null) { Marshal.ReleaseComObject(ppv); } } return new IDLWrapper(IntPtr.Zero); }
public static TInterface GetFolder <TInterface>(this IFolderView folderView) { folderView.GetFolder(typeof(TInterface).GUID, out var ppv); return((TInterface)ppv); }
/// <summary> /// Open folder and select an item. /// </summary> /// <remarks> /// SHParseDisplayName will not always find the correct folder. If the user has a folder open that is rooted in their user folder (e.g. the desktop, Dropbox/Mega/Nextcloud folder), /// this won't match the folder reference returned by SHParseDisplayName if given the actual path of the same folder. This will result in opening a duplicate folder. /// So instead, we iterate through open folder windows for a path match first, then use the old method if one is not found. /// </remarks> /// <param name="filePath">Full path to a file to highlight in Windows Explorer</param> public static void OpenFolderAndSelectItem(string filePath) { var folderPath = Path.GetDirectoryName(filePath); bool opened = false; var shellWindowsType = Type.GetTypeFromCLSID(SID_ShellWindows); var shellWindows = (dynamic)Activator.CreateInstance(shellWindowsType); try { foreach (IServiceProvider sp in shellWindows) { var pidl = IntPtr.Zero; var nativeFile = IntPtr.Zero; try { sp.QueryService(SID_STopLevelBrowser, typeof(IShellBrowser).GUID, out object sb); IShellBrowser shellBrowser = (IShellBrowser)sb; shellBrowser.QueryActiveShellView(out object sv); IFolderView fv = sv as IFolderView; if (fv != null) { // only folder implementation support this (IE windows do not for example) fv.GetFolder(typeof(IPersistFolder2).GUID, out object pf); IPersistFolder2 persistFolder = (IPersistFolder2)pf; // get current folder pidl persistFolder.GetCurFolder(out pidl); var path = new StringBuilder(1024); if (SHGetPathFromIDList(pidl, path)) { if (string.Equals(path.ToString(), folderPath, StringComparison.InvariantCultureIgnoreCase)) { SHParseDisplayName(Path.Combine(folderPath, filePath), IntPtr.Zero, out nativeFile, 0, out _); IntPtr[] fileArray; if (nativeFile == IntPtr.Zero) { // Open the folder without the file selected if we can't find the file fileArray = new IntPtr[0]; } else { fileArray = new IntPtr[] { nativeFile }; } SHOpenFolderAndSelectItems(pidl, (uint)fileArray.Length, fileArray, 0); opened = true; break; } } } } finally { if (nativeFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(nativeFile); } if (pidl != IntPtr.Zero) { Marshal.FreeCoTaskMem(pidl); } Marshal.ReleaseComObject(sp); } } } finally { Marshal.FinalReleaseComObject(shellWindows); } if (!opened) { IntPtr nativeFolder; uint psfgaoOut; SHParseDisplayName(folderPath, IntPtr.Zero, out nativeFolder, 0, out psfgaoOut); if (nativeFolder == IntPtr.Zero) { // Log error, can't find folder return; } IntPtr nativeFile; SHParseDisplayName(Path.Combine(folderPath, filePath), IntPtr.Zero, out nativeFile, 0, out psfgaoOut); IntPtr[] fileArray; if (nativeFile == IntPtr.Zero) { // Open the folder without the file selected if we can't find the file fileArray = new IntPtr[0]; } else { fileArray = new IntPtr[] { nativeFile }; } SHOpenFolderAndSelectItems(nativeFolder, (uint)fileArray.Length, fileArray, 0); Marshal.FreeCoTaskMem(nativeFolder); if (nativeFile != IntPtr.Zero) { Marshal.FreeCoTaskMem(nativeFile); } } }