我想根据文件名(含扩展名)取得文件的图标
该怎么做?

解决方案 »

  1.   

    我记得是用API的,你google找下,有这方面的源码的,以前做过一个。
      

  2.   

    想把取出的图标赋给一个可以显示图片的控件,假设AAA
    AAA.value = ..........;
      

  3.   


    1namespace MaLio.IO.Icon {
    2
    3  /// 
    4  /// reads icons from the windows shell
    5  /// 
    6  public class IconReader {
    7
    8    /// 
    9    /// used to get/return information about a file
    10    /// in SHGetFileInfo call 
    11    /// 
    12    [System.FlagsAttribute()]
    13    private enum EnumFileInfoFlags : uint {
    14      /// get large icon
    15      LARGEICON           = 0x000000000,      
    16      /// get small icon
    17      SMALLICON           = 0x000000001,      
    18      /// get open icon
    19      OPENICON            = 0x000000002,      
    20      /// get shell size icon
    21      SHELLICONSIZE       = 0x000000004,      
    22      /// pszPath is a pidl
    23      PIDL                = 0x000000008,      
    24      /// use passed dwFileAttribute
    25      USEFILEATTRIBUTES   = 0x000000010,      
    26      /// apply the appropriate overlays
    27      ADDOVERLAYS         = 0x000000020,      
    28      /// get the index of the overlay
    29      OVERLAYINDEX        = 0x000000040,      
    30      /// get icon
    31      ICON                = 0x000000100,      
    32      /// get display name
    33      DISPLAYNAME          = 0x000000200,      
    34      /// get type name
    35      TYPENAME            = 0x000000400,     
    36      /// get attributes
    37      ATTRIBUTES          = 0x000000800,      
    38      /// get icon location
    39      ICONLOCATION        = 0x000001000,      
    40      /// return exe type
    41      EXETYPE             = 0x000002000,      
    42      /// get system icon index
    43      SYSICONINDEX        = 0x000004000,      
    44      /// put a link overlay on icon
    45      LINKOVERLAY         = 0x000008000,      
    46      /// show icon in selected state
    47      SELECTED            = 0x000010000,      
    48      /// get only specified attributes
    49      ATTR_SPECIFIED      = 0x000020000       
    50    }
    51
    52    /// 
    53    /// maxumum length of path 
    54    /// 
    55    private const int conMAX_PATH = 260;
    56
    57    /// 
    58    /// looking for folder
    59    /// 
    60    private const uint conFILE_ATTRIBUTE_DIRECTORY = 0x00000010;  
    61
    62    /// 
    63    /// looking for file
    64    /// 
    65    private const uint conFILE_ATTRIBUTE_NORMAL = 0x00000080;  
    66
    67    /// 
    68    /// size of the icon
    69    /// 
    70    public enum EnumIconSize {
    71      /// 32x32
    72      Large = 0,
    73      /// 16x16
    74      Small = 1
    75    }
    76        
    77    /// 
    78    /// state of the folder
    79    /// 
    80    public enum EnumFolderType {
    81      /// open folder
    82      Open = 0,
    83      /// closed folder
    84      Closed = 1
    85    }
    86
    87    /// 
    88    /// hold file/icon information
    89    /// see platformSDK SHFILEINFO
    90    /// 
    91    /// 
    92    /// be sure to call DestroyIcon [hIcon] when done
    93    /// 
    94    [System.Runtime.InteropServices.StructLayout(
    95       System.Runtime.InteropServices.LayoutKind.Sequential)]
    96    private struct ShellFileInfo { 
    97
    98      public const int          conNameSize = 80;
    99      public System.IntPtr      hIcon;  // note to call DestroyIcon
    100      public int                iIndex; 
    101      public uint                dwAttributes; 
    102
    103      [System.Runtime.InteropServices.MarshalAs(
    104         System.Runtime.InteropServices.UnmanagedType.ByValTStr, 
    105         SizeConst=conMAX_PATH)]
    106      public string              szDisplayName; 
    107
    108      [System.Runtime.InteropServices.MarshalAs(
    109         System.Runtime.InteropServices.UnmanagedType.ByValTStr, 
    110         SizeConst=conNameSize)]
    111      public string              szTypeName; 
    112    };
    113
    114    /// 
    115    /// used to free a windows icon handle
    116    /// 
    117    /// "hIcon">icon handle.
    118    [System.Runtime.InteropServices.DllImport("User32.dll")]
    119    private static extern int DestroyIcon(System.IntPtr hIcon);
    120
    121    /// 
    122    /// gets file information 
    123    /// see platformSDK
    124    /// 
    125    [System.Runtime.InteropServices.DllImport("Shell32.dll")]
    126    private static extern System.IntPtr SHGetFileInfo(
    127      string pszPath,
    128      uint dwFileAttributes,
    129      ref ShellFileInfo psfi,
    130      uint cbFileInfo,
    131      uint uFlags
    132    );
    133
    134    /// 
    135    /// lookup and return an icon from windows shell
    136    /// 
    137    /// "name">path to the file
    138    /// "size">large or small
    139    /// "linkOverlay">true to include the overlay link iconlet
    140    /// requested icon
    141    public static System.Drawing.Icon GetFileIcon(
    142      string filePath, 
    143      EnumIconSize size, 
    144      bool addLinkOverlay) 
    145    {
    146
    147      EnumFileInfoFlags flags = 
    148        EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES;
    149
    150      // add link overlay if requested
    151      if (addLinkOverlay) {
    152        flags |= EnumFileInfoFlags.LINKOVERLAY;
    153      }
    154
    155      // set size
    156      if (size == EnumIconSize.Small) {
    157        flags |= EnumFileInfoFlags.SMALLICON;
    158      }
    159      else {
    160        flags |= EnumFileInfoFlags.LARGEICON;
    161      }
    162
    163      ShellFileInfo shellFileInfo = new ShellFileInfo();
    164
    165      SHGetFileInfo(
    166        filePath, 
    167        conFILE_ATTRIBUTE_NORMAL, 
    168        ref shellFileInfo, 
    169        (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), 
    170        (uint)flags);
    171
    172      // deep copy 
    173      System.Drawing.Icon icon = 
    174      (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone();
    175
    176      // release handle 
    177      DestroyIcon(shellFileInfo.hIcon);    
    178
    179      return icon;
    180    }
    181
    182    /// 
    183    ///  lookup and return an icon from windows shell
    184    /// 
    185    /// "size">large or small
    186    /// "folderType">open or closed
    187    /// requested icon
    188    public static System.Drawing.Icon GetFolderIcon(
    189      EnumIconSize size, 
    190      EnumFolderType folderType) 
    191    {
    192      return GetFolderIcon(null, size, folderType);
    193    }
    194
    195    /// 
    196    /// lookup and return an icon from windows shell
    197    /// 
    198    /// "folderPath">path to folder    
    199    /// "size">large or small
    200    /// "folderType">open or closed
    201    /// requested icon
    202    public static System.Drawing.Icon GetFolderIcon(
    203      string folderPath, 
    204      EnumIconSize size, 
    205      EnumFolderType folderType) 
    206    {
    207      
    208      EnumFileInfoFlags flags = 
    209        EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES;
    210
    211      if (folderType == EnumFolderType.Open) {
    212        flags |= EnumFileInfoFlags.OPENICON;
    213      }
    214      
    215      if (EnumIconSize.Small == size) {
    216        flags |= EnumFileInfoFlags.SMALLICON;
    217      } 
    218      else {
    219        flags |= EnumFileInfoFlags.LARGEICON;
    220      }
    221      
    222      ShellFileInfo shellFileInfo = new ShellFileInfo();
    223      SHGetFileInfo(
    224        folderPath, 
    225        conFILE_ATTRIBUTE_DIRECTORY, 
    226        ref shellFileInfo, 
    227        (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), 
    228        (uint)flags );    
    229
    230      // deep copy 
    231      System.Drawing.Icon icon = 
    232        (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone();
    233
    234      // release handle
    235      DestroyIcon(shellFileInfo.hIcon);  
    236
    237      return icon;
    238    }  
    239  }
    240}