Changeset 135


Ignore:
Timestamp:
3/8/2008 4:15:59 AM (6 years ago)
Author:
lowjoel
Message:

Use SHGetFileInfo to get not only the description but also the icon for the file.

Location:
branches/eraser6/Util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Util/File.cs

    r129 r135  
    22using System.Collections.Generic; 
    33using System.Text; 
     4 
    45using System.Runtime.InteropServices; 
     6using System.Drawing; 
    57 
    68namespace Eraser.Util 
    79{ 
    8     using HICON = UIntPtr; 
    9     using HIMAGELIST = UIntPtr; 
     10    using HICON = IntPtr; 
     11    using HIMAGELIST = IntPtr; 
    1012 
    1113    public class File 
     
    2123        { 
    2224            SHFILEINFO shfi = new SHFILEINFO(); 
    23             HIMAGELIST imageList = (HIMAGELIST) SHGetFileInfo(path, 0, ref shfi, 
    24                 Marshal.SizeOf(shfi), SHGetFileInfoFlags.SHGFI_SMALLICON | 
    25                 SHGetFileInfoFlags.SHGFI_SYSICONINDEX | 
     25            SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 
    2626                SHGetFileInfoFlags.SHGFI_DISPLAYNAME); 
    2727            return shfi.szDisplayName; 
     28        } 
     29 
     30        /// <summary> 
     31        /// Uses SHGetFileInfo to retrieve the icon for the given file, folder or 
     32        /// drive. 
     33        /// </summary> 
     34        /// <param name="path">A string that contains the path and file name for 
     35        /// the file in question. Both absolute and relative paths are valid.</param> 
     36        /// <returns>An Icon object containing the bitmap</returns> 
     37        public static Icon GetFileIcon(string path) 
     38        { 
     39            SHFILEINFO shfi = new SHFILEINFO(); 
     40            SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 
     41                SHGetFileInfoFlags.SHGFI_SMALLICON | SHGetFileInfoFlags.SHGFI_ICON); 
     42            return Icon.FromHandle(shfi.hIcon); 
    2843        } 
    2944 
     
    7691        /// </returns> 
    7792        [DllImport("Shell32.dll")] 
    78         private static extern UIntPtr SHGetFileInfo(string path, uint fileAttributes, 
     93        private static extern IntPtr SHGetFileInfo(string path, uint fileAttributes, 
    7994            ref SHFILEINFO psfi, int fileInfo, SHGetFileInfoFlags flags); 
    8095 
  • branches/eraser6/Util/Util.csproj

    r129 r135  
    3131    <Reference Include="System" /> 
    3232    <Reference Include="System.Data" /> 
     33    <Reference Include="System.Drawing" /> 
    3334    <Reference Include="System.Xml" /> 
    3435  </ItemGroup> 
Note: See TracChangeset for help on using the changeset viewer.