private static void GetChilds(IShellFolder desktop) { Console.WriteLine("===============GetChilds================"); //循环查找子项 Microsoft.WindowsAPICodePack.Shell.IEnumIDList Enum = null; IEnumIDList EnumPtr; IntPtr pidlSub; uint celtFetched; if (desktop.EnumObjects(IntPtr.Zero, ShellNativeMethods.ShellFolderEnumerationOptions.Folders | ShellNativeMethods.ShellFolderEnumerationOptions.IncludeHidden | ShellNativeMethods.ShellFolderEnumerationOptions.Storage, out EnumPtr) == CONST.S_OK) { while (EnumPtr.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == CONST.S_FALSE) { IntPtr strr; desktop.GetDisplayNameOf(ref pidlSub, (uint)SHGNO.INFOLDER, out strr); string name = Marshal.PtrToStringAuto(strr); //SHFILEINFO info = new SHFILEINFO(); //Shell32.SHGetFileInfo(pidlSub, 0, ref info, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI.PIDL | SHGFI.DISPLAYNAME | SHGFI.ICONLOCATION); //IShellFolder iSub; //desktop.BindToObject(pidlSub, IntPtr.Zero, ref Guids.IID_IShellFolder, out iSub); Console.WriteLine(name); } } Console.WriteLine(); Console.WriteLine(); }
private void button1_Click(object sender, EventArgs e) { // get desktop folder IntPtr shellFolderPtr; ShellAPI.SHGetDesktopFolder(out shellFolderPtr); IShellFolder shellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(shellFolderPtr, typeof(IShellFolder)); listView1.Items.Clear(); ShellAPI.SHCONTF fileFlag = ShellAPI.SHCONTF.NONFOLDERS | ShellAPI.SHCONTF.FOLDERS; IntPtr fileEnumPtr = IntPtr.Zero; if (shellFolder.EnumObjects(IntPtr.Zero, fileFlag, out fileEnumPtr) == ShellAPI.S_OK) { IEnumIDList fileEnum = (IEnumIDList)Marshal.GetTypedObjectForIUnknown(fileEnumPtr, typeof(IEnumIDList)); IntPtr gelt; int celtFetched; while (fileEnum.Next(1, out gelt, out celtFetched) == ShellAPI.S_OK && celtFetched == 1) { ShellAPI.SHFILEINFO info = new ShellAPI.SHFILEINFO(); ShellAPI.SHGetFileInfo(gelt, 0, ref info, ShellAPI.cbFileInfo, ShellAPI.SHGFI.DISPLAYNAME | ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.TYPENAME); listView1.Items.Add(info.szDisplayName); } } }
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) { #region 判断节点是否已经展开 if (e.Node.Nodes.Count != 1) { return; } else { if (e.Node.FirstNode.Text != "...") { return; } } e.Node.Nodes.Clear(); #endregion try // 因为文件夹安全设置和设备准备问题,可能导致失败。 { ShellItem sItem = (ShellItem)e.Node.Tag; IShellFolder root = sItem.ShellFolder; //循环查找子项 IEnumIDList Enum = null; IntPtr EnumPtr = IntPtr.Zero; IntPtr pidlSub; int celtFetched; if (root.EnumObjects(this.Handle, SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN, out EnumPtr) == API.S_OK) { Enum = (IEnumIDList)Marshal.GetObjectForIUnknown(EnumPtr); while (Enum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == API.S_FALSE) { string name = API.GetNameByIShell(root, pidlSub); IShellFolder iSub; try { root.BindToObject(pidlSub, IntPtr.Zero, ref Guids.IID_IShellFolder, out iSub); } catch (Exception ex) { MessageBox.Show(ex.Message + " : " + name); continue; } ShellItem shellItem = new ShellItem(pidlSub, iSub, sItem); int imgIndex = API.GetSmallIconIndex(shellItem.PIDLFull.Ptr); TreeNode nodeSub = new TreeNode(name, imgIndex, imgIndex); nodeSub.Tag = shellItem; nodeSub.Nodes.Add("..."); e.Node.Nodes.Add(nodeSub); } } }catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static IEnumIDList GetIEnumIDList(IShellFolder folder, SHCONTF flags, out HResult navResult) { IEnumIDList result; var res = folder.EnumObjects(IsCareForMessageHandle ? MessageHandle : IntPtr.Zero, flags, out result); navResult = res; return(res == HResult.S_OK ? result : null); }
private static IEnumIDList GetIEnumIDList(IShellFolder folder, SHCONTF flags) { try { return(folder.EnumObjects(IntPtr.Zero, flags)); } catch { } return(null); }
private static IEnumIDList GetIEnumIdList(IShellFolder folder, SHCONTF flags) { IEnumIDList result; if (folder.EnumObjects(IntPtr.Zero, flags, out result) == HResult.S_OK) { return(result); } return(null); }
/// <summary> /// Enumerates all children of this item. If this item is not a folder/container, this method will return an empty enumeration. /// </summary> /// <param name="filter">A filter for the types of children to enumerate.</param> /// <param name="parentWindow">The parent window.</param> /// <returns>An enumerated list of children matching the filter.</returns> public IEnumerable <ShellItem> EnumerateChildren(FolderItemFilter filter /*= FolderItemFilter.Folders | FolderItemFilter.IncludeHidden | FolderItemFilter.NonFolders | FolderItemFilter.IncludeSuperHidden */, HWND parentWindow = default) { if (iShellFolder.EnumObjects(parentWindow, (SHCONTF)filter, out var eo).Failed) { Debug.WriteLine($"Unable to enum children in folder."); } foreach (var p in eo.Enumerate(20)) { ShellItem i = null; try { i = this[p]; } catch (Exception e) { Debug.WriteLine($"Unable to open folder child: {e.Message}"); } if (i != null) { yield return(i); } } Marshal.ReleaseComObject(eo); }
/// <summary> /// 获取非文件夹的文件 /// </summary> /// <param name="root">根目录</param> /// <param name="handle">当前窗口句柄</param> /// <param name="sItem">当前项</param> /// <returns></returns> public List <ShellItem> GetNonFolders(IShellFolder root, IntPtr handle, ShellItem sItem) { List <ShellItem> allFileShellItem = new List <ShellItem>();//文件的集合 if (root.EnumObjects(handle, SHCONTF.NONFOLDERS | SHCONTF.INCLUDEHIDDEN, out EnumPtr) == API.S_OK) { Enum = (IEnumIDList)Marshal.GetObjectForIUnknown(EnumPtr); IntPtr pidlSub; int celtFetched; while (Enum.Next(1, out pidlSub, out celtFetched) == 0 && celtFetched == API.S_FALSE) { var shellItem = new ShellItem(pidlSub, null, sItem); allFileShellItem.Add(shellItem); } } return(allFileShellItem); }
/// <summary> /// Enumerates all children of this item. If this item is not a folder/container, this method will return an empty enumeration. /// </summary> /// <param name="filter">A filter for the types of children to enumerate.</param> /// <param name="parentWindow">The parent window.</param> /// <returns>An enumerated list of children matching the filter.</returns> public IEnumerable <ShellItem> EnumerateChildren(FolderItemFilter filter /*= FolderItemFilter.Folders | FolderItemFilter.IncludeHidden | FolderItemFilter.NonFolders | FolderItemFilter.IncludeSuperHidden */, System.Windows.Forms.IWin32Window parentWindow = null) { IEnumIDList eo = null; try { eo = iShellFolder.EnumObjects(IWin2Ptr(parentWindow, false), (SHCONTF)filter); } catch (Exception e) { Debug.WriteLine($"Unable to enum children in folder: {e.Message}"); } foreach (var p in eo.Enumerate(20)) { ShellItem i = null; try { i = this[p]; } catch (Exception e) { Debug.WriteLine($"Unable to open folder child: {e.Message}"); } if (i != null) { yield return(i); } } Marshal.ReleaseComObject(eo); }
public static IEnumerable <PITEMID_CHILD> Enumerate(this IShellFolder sf) { IEnumIDList ppenumIDList; sf.EnumObjects(IntPtr.Zero, 0x00020 /* SHCONTF_FOLDERS */ | 0x00040 /* SHCONTF_NONFOLDERS */, out ppenumIDList); if (ppenumIDList == null) { yield break; } var items = new PITEMID_CHILD[1]; while (true) { int fetched; ppenumIDList.Next(items.Length, items, out fetched); if (fetched == 0) { break; } yield return(items[0]); } }
/// <summary> /// Enumerates all children of this item. If this item is not a folder/container, this method will return an empty enumeration. /// </summary> /// <param name="filter">A filter for the types of children to enumerate.</param> /// <param name="parentWindow">The parent window.</param> /// <returns>An enumerated list of children matching the filter.</returns> public IEnumerable <ShellItem> EnumerateChildren(FolderItemFilter filter /*= FolderItemFilter.Folders | FolderItemFilter.IncludeHidden | FolderItemFilter.NonFolders | FolderItemFilter.IncludeSuperHidden */, System.Windows.Forms.IWin32Window parentWindow = null) { IEnumIDList eo = null; try { eo = iShellFolder.EnumObjects(IWin2Ptr(parentWindow, false), (SHCONTF)filter); } catch (Exception e) { Debug.WriteLine($"Unable to enum children in folder: {e.Message}"); } if (eo != null) { foreach (var p in new Collections.IEnumFromNext <IntPtr>((out IntPtr p) => eo.Next(1, out p, out var f).Succeeded&& f == 1, () => { try { eo.Reset(); } catch { } })) { ShellItem i = null; try { i = this[new PIDL(p)]; } catch (Exception e) { Debug.WriteLine($"Unable to open folder child: {e.Message}"); } if (i != null) { yield return(i); } } Marshal.ReleaseComObject(eo); } }
public Bitmap GetThumbnail(string fileName) { if (!File.Exists(fileName) && !Directory.Exists(fileName)) { throw new FileNotFoundException(string.Format("The file '{0}' does not exist", fileName), fileName); } if (this.thumbNail != null) { this.thumbNail.Dispose(); this.thumbNail = null; } IShellFolder folder = null; try { folder = this.DesktopFolder; } catch (Exception ex) { throw ex; } if (folder != null) { IntPtr pidlMain = IntPtr.Zero; try { int parsed = 0; int pdwAttrib = 0; string filePath = Path.GetDirectoryName(fileName); folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, ref parsed, ref pidlMain, ref pdwAttrib); } catch (Exception ex) { Marshal.ReleaseComObject(folder); throw ex; } if (pidlMain != IntPtr.Zero) { Guid iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); IShellFolder item = null; try { folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item); } catch (Exception ex) { Marshal.ReleaseComObject(folder); this.Allocator.Free(pidlMain); throw ex; } if (item != null) { IEnumIDList idEnum = null; try { item.EnumObjects(IntPtr.Zero, (ESHCONTF.SHCONTF_FOLDERS | ESHCONTF.SHCONTF_NONFOLDERS), ref idEnum); } catch (Exception ex) { Marshal.ReleaseComObject(folder); this.Allocator.Free(pidlMain); throw ex; } if (idEnum != null) { int res = 0; IntPtr pidl = IntPtr.Zero; int fetched = 0; bool complete = false; while (!complete) { res = idEnum.Next(1, ref pidl, ref fetched); if (res != 0) { pidl = IntPtr.Zero; complete = true; } else { if (this.GetThumbnailHelper(fileName, pidl, item)) { complete = true; } } if (pidl != IntPtr.Zero) { this.Allocator.Free(pidl); } } Marshal.ReleaseComObject(idEnum); } Marshal.ReleaseComObject(item); } this.Allocator.Free(pidlMain); } Marshal.ReleaseComObject(folder); } return(this.ThumbNail); }
public int EnumObjects(IntPtr hwnd, ShellAPI.SHCONTF grfFlags, out IntPtr enumIDList) { checkDisposed(); return(_iShellFolder.EnumObjects(hwnd, grfFlags, out enumIDList)); }
public Bitmap GetThumbnail(string fileName) { if (!File.Exists(fileName) && !Directory.Exists(fileName)) { throw new FileNotFoundException(string.Format("The file '{0}' does not exist", fileName), fileName); } if (ThumbNail != null) { ThumbNail.Dispose(); ThumbNail = null; } var folder = getDesktopFolder; if (folder == null) { return(ThumbNail); } var pidlMain = IntPtr.Zero; try { var cParsed = 0; var pdwAttrib = 0; var filePath = Path.GetDirectoryName(fileName); folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, ref cParsed, ref pidlMain, ref pdwAttrib); } catch (Exception) { freeResources(pidlMain, folder, null, null); throw; } if (pidlMain == IntPtr.Zero) { freeResources(pidlMain, folder, null, null); return(null); } var iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); IShellFolder item = null; try { folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item); } catch (Exception) { freeResources(pidlMain, folder, null, item); throw; } if (item == null) { freeResources(pidlMain, folder, null, null); return(null); } IEnumIDList idEnum = null; try { item.EnumObjects(IntPtr.Zero, (Eshcontf.ShcontfFolders | Eshcontf.ShcontfNonfolders), ref idEnum); } catch (Exception) { freeResources(pidlMain, folder, idEnum, item); throw; } if (idEnum == null) { freeResources(pidlMain, folder, null, item); return(null); } var pidl = IntPtr.Zero; var fetched = 0; var complete = false; while (!complete) { var hRes = idEnum.Next(1, ref pidl, ref fetched); if (hRes != 0) { pidl = IntPtr.Zero; complete = true; } else { if (getThumbNail(fileName, pidl, item)) { complete = true; } } if (pidl != IntPtr.Zero) { allocator.Free(pidl); } } freeResources(pidlMain, folder, idEnum, item); return(ThumbNail); }
public System.Drawing.Bitmap GetThumbnail(string file, int width, int height) { if ((!File.Exists(file)) && (!Directory.Exists(file))) { throw new FileNotFoundException( String.Format("The file '{0}' does not exist", file), file); } if (thumbNail != null) { thumbNail.Dispose(); thumbNail = null; } IShellFolder folder = null; try { folder = GetDesktopFolder; } catch (Exception ex) { throw ex; } if (folder != null) { IntPtr pidlMain = IntPtr.Zero; try { int cParsed = 0; int pdwAttrib = 0; string filePath = Path.GetDirectoryName(file); pidlMain = IntPtr.Zero; folder.ParseDisplayName( IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlMain, out pdwAttrib); } catch (Exception ex) { Marshal.ReleaseComObject(folder); throw ex; } if (pidlMain != IntPtr.Zero) { // IShellFolder: Guid iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); IShellFolder item = null; try { folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item); } catch (Exception ex) { Marshal.ReleaseComObject(folder); UnmanagedMethods.CoTaskMemFree(pidlMain); throw ex; } if (item != null) { IEnumIDList idEnum = null; try { item.EnumObjects( IntPtr.Zero, (ESHCONTF.SHCONTF_FOLDERS | ESHCONTF.SHCONTF_NONFOLDERS), ref idEnum); } catch (Exception ex) { Marshal.ReleaseComObject(folder); UnmanagedMethods.CoTaskMemFree(pidlMain); throw ex; } if (idEnum != null) { int hRes = 0; IntPtr pidl = IntPtr.Zero; int fetched = 0; bool complete = false; while (!complete) { hRes = idEnum.Next(1, ref pidl, out fetched); if (hRes != 0) { pidl = IntPtr.Zero; complete = true; } else { if (GetThumbnail(file, pidl, item, width, height)) { complete = true; } } if (pidl != IntPtr.Zero) { UnmanagedMethods.CoTaskMemFree(pidl); } } Marshal.ReleaseComObject(idEnum); } Marshal.ReleaseComObject(item); } UnmanagedMethods.CoTaskMemFree(pidlMain); } Marshal.ReleaseComObject(folder); } return(thumbNail); }
public static List <ShellItem> GetItems(Guid FolderID) //Получает список элементов каталога по GUID { IntPtr p = IntPtr.Zero; IShellFolder pFolder = null; IEnumIDList pEnum = null; IntPtr pItem = IntPtr.Zero; IntPtr lpStr = IntPtr.Zero; STRRET strret; Guid guid = typeof(IShellFolder).GUID; List <ShellItem> items = new List <ShellItem>(); ShellItem si; try { int hr = NativeMethods.SHGetKnownFolderIDList(ref FolderID, 0, IntPtr.Zero, out p); if (hr != 0) { throw Marshal.GetExceptionForHR(hr); } hr = NativeMethods.SHBindToObject(null, p, null, ref guid, out pFolder); if (hr != 0) { throw Marshal.GetExceptionForHR(hr); } pFolder.EnumObjects(IntPtr.Zero, NativeMethods.SHCONTF_FOLDERS | NativeMethods.SHCONTF_NONFOLDERS, out pEnum); while (true) { pItem = IntPtr.Zero; uint res = pEnum.Next(1, out pItem, IntPtr.Zero); if (res != 0) { break; } si = new ShellItem(); //display name lpStr = IntPtr.Zero; strret = new STRRET(); pFolder.GetDisplayNameOf(pItem, NativeMethods.SHGDN_NORMAL, out strret); hr = NativeMethods.StrRetToStr(ref strret, pItem, out lpStr); if (hr != 0) { throw Marshal.GetExceptionForHR(hr); } string s = Marshal.PtrToStringUni(lpStr); si.DisplayName = s; NativeMethods.CoTaskMemFree(lpStr); //path lpStr = IntPtr.Zero; strret = new STRRET(); pFolder.GetDisplayNameOf(pItem, NativeMethods.SHGDN_FORPARSING, out strret); hr = NativeMethods.StrRetToStr(ref strret, pItem, out lpStr); if (hr != 0) { throw Marshal.GetExceptionForHR(hr); } s = Marshal.PtrToStringUni(lpStr); try { si.Path = new Uri(s); } catch (UriFormatException) { si.Path = new Uri("file://localhost/" + s); } NativeMethods.CoTaskMemFree(lpStr); //icon try { Guid iid_IIExtractIcon = typeof(IExtractIcon).GUID; IExtractIcon pExtract; pFolder.GetUIObjectOf(IntPtr.Zero, 1, new IntPtr[] { pItem }, ref iid_IIExtractIcon, 0, out pExtract); StringBuilder sbIcon = new StringBuilder(260); int index = 0; uint flags; hr = pExtract.GetIconLocation(NativeMethods.GIL_FORSHELL, sbIcon, 260, out index, out flags); if (hr == 0) { IntPtr hIconSmall = IntPtr.Zero, hIconLarge = IntPtr.Zero; hr = pExtract.Extract(sbIcon.ToString(), (uint)index, out hIconLarge, out hIconSmall, 0x00140014); if (hr == 0 && hIconSmall != IntPtr.Zero) { var icon = System.Drawing.Icon.FromHandle(hIconSmall); var bitmap = icon.ToBitmap(); using (bitmap) { MemoryStream ms = new MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); si.Image = ms.ToArray(); } NativeMethods.DestroyIcon(hIconSmall); NativeMethods.DestroyIcon(hIconLarge); } else { si.Image = new byte[0]; } } else { si.Image = new byte[0]; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); si.Image = new byte[0]; } items.Add(si); NativeMethods.CoTaskMemFree(pItem); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), ex.GetType().ToString()); } finally { if (p != IntPtr.Zero) { NativeMethods.CoTaskMemFree(p); } if (pFolder != null) { Marshal.ReleaseComObject(pFolder); } if (pEnum != null) { Marshal.ReleaseComObject(pEnum); } } return(items); }