public static string GetPidlDisplayName(IntPtr pidl) { SHFILEINFO fileInfo = new SHFILEINFO(); Shell32.SHGetFileInfo(pidl, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo), SHGFI.SHGFI_PIDL | SHGFI.SHGFI_DISPLAYNAME); return fileInfo.szDisplayName; }
public static extern IntPtr SHGetFileInfo(IntPtr pszPath, uint dwFileAttribs, out SHFILEINFO psfi, uint cbFileInfo, SHGFI uFlags);
/// <summary> /// Creates the desktop shell folder. /// </summary> /// <returns>The desktop shell folder.</returns> private static ShellItem CreateDesktopShellFolder() { // Get the desktop shell folder interface. IShellFolder desktopShellFolderInterface = null; var result = Shell32.SHGetDesktopFolder(out desktopShellFolderInterface); // Validate the result. if (result != 0) { // Throw the failure as an exception. Marshal.ThrowExceptionForHR(result); } // Get the dekstop PDIL. var desktopPIDL = IntPtr.Zero; result = Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, CSIDL.CSIDL_DESKTOP, ref desktopPIDL); // Validate the result. if (result != 0) { // Throw the failure as an exception. Marshal.ThrowExceptionForHR(result); } // Get the file info. var fileInfo = new SHFILEINFO(); Shell32.SHGetFileInfo(desktopPIDL, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo), SHGFI.SHGFI_DISPLAYNAME | SHGFI.SHGFI_PIDL | SHGFI.SHGFI_SMALLICON | SHGFI.SHGFI_SYSICONINDEX); // Return the Shell Folder. return new ShellItem { DisplayName = fileInfo.szDisplayName, IconIndex = fileInfo.iIcon, HasSubFolders = true, IsFolder = true, ShellFolderInterface = desktopShellFolderInterface, PIDL = desktopPIDL, RelativePIDL = desktopPIDL }; }
/// <summary> /// Initialises the ShellItem, from its PIDL and parent. /// </summary> /// <param name="pidl">The pidl.</param> /// <param name="parentFolder">The parent folder.</param> private void Initialise(IntPtr pidl, ShellItem parentFolder) { // Set the parent item and relative pidl. ParentItem = parentFolder; RelativePIDL = pidl; // Create the fully qualified PIDL. PIDL = Shell32.ILCombine(parentFolder.PIDL, pidl); // Use the desktop folder to get attributes. var flags = SFGAO.SFGAO_FOLDER | SFGAO.SFGAO_HASSUBFOLDER | SFGAO.SFGAO_BROWSABLE | SFGAO.SFGAO_FILESYSTEM; //todo was this parentFolder.ShellFolderInterface.GetAttributesOf(1, ref pidl, ref flags); var apidl = Marshal.AllocCoTaskMem(IntPtr.Size*1); Marshal.Copy(new IntPtr[] {pidl}, 0, apidl, 1); parentFolder.ShellFolderInterface.GetAttributesOf(1, apidl, ref flags); IsFolder = (flags & SFGAO.SFGAO_FOLDER) != 0; HasSubFolders = (flags & SFGAO.SFGAO_HASSUBFOLDER) != 0; // Get the file info. var fileInfo = new SHFILEINFO(); Shell32.SHGetFileInfo(PIDL, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo), SHGFI.SHGFI_SMALLICON | SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_PIDL | SHGFI.SHGFI_DISPLAYNAME | SHGFI.SHGFI_ATTRIBUTES); // Set extended attributes. DisplayName = fileInfo.szDisplayName; Attributes = (SFGAO)fileInfo.dwAttributes; TypeName = fileInfo.szTypeName; IconIndex = fileInfo.iIcon; // Are we a folder? if (IsFolder) { // Bind the shell folder interface. IShellFolder shellFolderInterface; IntPtr ppv = IntPtr.Zero; var result = parentFolder.ShellFolderInterface.BindToObject(pidl, IntPtr.Zero, ref Shell32.IID_IShellFolder, out ppv);//out shellFolderInterface); shellFolderInterface = ((IShellFolder) Marshal.GetObjectForIUnknown(ppv)); ShellFolderInterface = shellFolderInterface; // Validate the result. if (result != 0) { // Throw the failure as an exception. Marshal.ThrowExceptionForHR((int)result); } } }
/// <summary> /// Creates the icon. /// </summary> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> private Icon CreateOverlayIcon() { var fileInfo = new SHFILEINFO(); Shell32.SHGetFileInfo(PIDL, 0, out fileInfo, (uint)Marshal.SizeOf(fileInfo), SHGFI.SHGFI_PIDL | SHGFI.SHGFI_ICON | SHGFI.SHGFI_ADDOVERLAYS); return fileInfo.hIcon != IntPtr.Zero ? Icon.FromHandle(fileInfo.hIcon) : null; }