public static Icon GetIcon(string path) { var directoryName = Path.GetDirectoryName(path); var fileName = Path.GetFileName(path); IntPtr folderPtr; if (SHGetDesktopFolder(out folderPtr) != SOk) { return(null); } var desktopFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(folderPtr, typeof(IShellFolder)); try { Sfgao pdwAttributes = 0; uint pchEaten = 0; IntPtr ppidl; desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, directoryName, ref pchEaten, out ppidl, ref pdwAttributes); Guid iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); IntPtr ppv; desktopFolder.BindToObject(ppidl, IntPtr.Zero, iidShellFolder, out ppv); var folder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(ppv, typeof(IShellFolder)); try { folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, fileName, ref pchEaten, out ppidl, ref pdwAttributes); try { return(ExtractIcon(folder, ppidl)); } finally { Marshal.Release(ppidl); } } finally { Marshal.Release(ppv); Marshal.ReleaseComObject(folder); } } finally { Marshal.Release(folderPtr); Marshal.ReleaseComObject(desktopFolder); } }
/// <summary> /// Gets the parent folder /// </summary> /// <param name="folderName">Folder path</param> /// <returns>IShellFolder for the folder (relative from the desktop)</returns> private IShellFolder GetParentFolder(string folderName) { if (null != _oParentFolder) { return(_oParentFolder); } var oDesktopFolder = GetDesktopFolder(); if (null == oDesktopFolder) { return(null); } // Get the PIDL for the folder file is in IntPtr pPidl; uint pchEaten = 0; Sfgao pdwAttributes = 0; var nResult = oDesktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, ref pchEaten, out pPidl, ref pdwAttributes); if (S_OK != nResult) { return(null); } var pStrRet = Marshal.AllocCoTaskMem(MAX_PATH * 2 + 4); Marshal.WriteInt32(pStrRet, 0, 0); _oDesktopFolder.GetDisplayNameOf(pPidl, Shgno.ForParsing, pStrRet); var strFolder = new StringBuilder(MAX_PATH); StrRetToBuf(pStrRet, pPidl, strFolder, MAX_PATH); Marshal.FreeCoTaskMem(pStrRet); _strParentFolder = strFolder.ToString(); // Get the IShellFolder for folder IntPtr pUnknownParentFolder; nResult = oDesktopFolder.BindToObject(pPidl, IntPtr.Zero, ref _iidIShellFolder, out pUnknownParentFolder); // Free the PIDL first Marshal.FreeCoTaskMem(pPidl); if (S_OK != nResult) { return(null); } _oParentFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnknownParentFolder, typeof(IShellFolder)); return(_oParentFolder); }
/// <summary> /// Get the PIDLs /// </summary> /// <param name="arrFi">Array of FileInfo</param> /// <returns>Array of PIDLs</returns> protected IntPtr[] GetPidLs(FileInfo[] arrFi) { if (null == arrFi || 0 == arrFi.Length) { return(null); } var oParentFolder = GetParentFolder(arrFi[0].DirectoryName); if (null == oParentFolder) { return(null); } var arrPidLs = new IntPtr[arrFi.Length]; var n = 0; foreach (var fi in arrFi) { // Get the file relative to folder uint pchEaten = 0; Sfgao pdwAttributes = 0; IntPtr pPidl; var nResult = oParentFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, fi.Name, ref pchEaten, out pPidl, ref pdwAttributes); if (S_OK != nResult) { FreePidLs(arrPidLs); return(null); } arrPidLs[n] = pPidl; n++; } return(arrPidLs); }