/// <summary> /// Returns a Image from a file system path /// </summary> /// <param name="path">The path can be a fully qualified path name, non-existent file, or file extension</param> /// <param name="size">The size of the bitmap to return</param> /// <param name="style">The style of the bitmap to retrieve</param> /// <param name="attributes">Extra attributes to specify for the path</param> /// <returns></returns> public static Image GetImageFromPath(string path, IconSizes size, IconStyles style, FileAttributes attributes) { SHFILEINFO shfi = new SHFILEINFO(); uint uFlags = SHGFI_ICON; uFlags |= (uint)size; uFlags |= (uint)style; if (attributes != 0) uFlags |= SHGFI_USEFILEATTRIBUTES; SHGetFileInfo(path, (int)attributes, ref shfi, (uint)Marshal.SizeOf(shfi), uFlags); Image image = (Image)Bitmap.FromHicon(shfi.hIcon).Clone(); DeleteObject(shfi.hIcon); return image; }
/// <summary> /// Gets the display name for the path specified /// </summary> /// <param name="filename"></param> /// <returns></returns> public static string GetDisplayName(string path) { SHFILEINFO shfi = new SHFILEINFO(); IntPtr p = SHGetFileInfo(path, (int)FileAttributes.Normal, ref shfi, (uint)Marshal.SizeOf(shfi), (uint)(SHGFI_DISPLAYNAME | SHGFI_USEFILEATTRIBUTES)); return shfi.szDisplayName; }
private static extern IntPtr SHGetFileInfo(string pszPath, int dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);