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); }
public static extern int StrRetToStr(ref STRRET pstr, IntPtr pidl, out IntPtr ppsz);