谢谢。

解决方案 »

  1.   

    可以考虑使用图标读取工具,如反编译获取图标工具SuperIcon,去读取系统图标存放的DLL中获取图标
      

  2.   

    我刚刚解决了,从网上抄的,整理了一下:
    使用方法:
    private MyTools.FileIcon FileDetailIcon=null;
    /// <summary>
    /// 选择了一个用户后,更新当前用户所有的电子文件
    /// </summary>
    private void FreshDetail()
    {
    if(FileDetailIcon==null)
    {
    FileDetailIcon=new FileIcon();
    this.lsvDetail.SmallImageList=FileDetailIcon.SmallImageList;
    this.lsvDetail.LargeImageList=FileDetailIcon.LargeImageList;
    }
    ……
    ListViewItem item = new ListViewItem(row[0].ToString(),FileDetailIcon.GetIconIndexByFileName(row[0].ToString()));
    item.SubItems.Add(row[1].ToString());
    item.SubItems.Add(row[2].ToString());
    this.lsvDetail.Items.Add(item);
    }
      

  3.   

    实现代码:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    namespace MyTools
    { /// <summary>
    /// 根据文件名得到对应的图标,文件名一定有扩展名,本函数代码从网上复制而来,故没有详细的说明
    /// </summary>
    public class FileIcon
    {
    /// <summary>
    /// 小图标对应的ImageList列表
    /// </summary>
    public ImageList SmallImageList = new ImageList();
    /// <summary>
    /// 大图标对应的ImageList列表
    /// </summary>
    public ImageList LargeImageList = new ImageList();
    private IconListManager _iconListManager;
    public FileIcon()
    {
    SmallImageList.ColorDepth = ColorDepth.Depth32Bit;
    LargeImageList.ColorDepth = ColorDepth.Depth32Bit; SmallImageList.ImageSize = new System.Drawing.Size( 16, 16 );
    LargeImageList.ImageSize = new System.Drawing.Size( 32, 32 ); _iconListManager = new IconListManager( SmallImageList, LargeImageList );
    }
    /// <summary>
    /// 根据指定的文件名,得到该文件名对应的图标雄鹰
    /// </summary>
    /// <param name="FileName">文件名</param>
    /// <returns>图标索引</returns>
    public int GetIconIndexByFileName(string FileName)
    {
    return _iconListManager.AddFileIcon( FileName );
    }


    /// <summary>
    /// IconReader 的摘要说明。
    /// </summary>
    public class IconReader
    {
    /// <summary>
    /// Options to specify the size of icons to return.
    /// </summary>
    public enum IconSize
    {
    /// <summary>
    /// Specify large icon - 32 pixels by 32 pixels.
    /// </summary>
    Large = 0,
    /// <summary>
    /// Specify small icon - 16 pixels by 16 pixels.
    /// </summary>
    Small = 1
    }
            
    /// <summary>
    /// Options to specify whether folders should be in the open or closed state.
    /// </summary>
    public enum FolderType
    {
    /// <summary>
    /// Specify open folder.
    /// </summary>
    Open = 0,
    /// <summary>
    /// Specify closed folder.
    /// </summary>
    Closed = 1
    } /// <summary>
    /// Returns an icon for a given file - indicated by the name parameter.
    /// </summary>
    /// <param name="name">Pathname for file.</param>
    /// <param name="size">Large or small</param>
    /// <param name="linkOverlay">Whether to include the link icon</param>
    /// <returns>System.Drawing.Icon</returns>
    public static System.Drawing.Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
    {
    Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
    uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES; if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY; /* Check the size specified for return. */
    if (IconSize.Small == size)
    {
    flags += Shell32.SHGFI_SMALLICON ;

    else 
    {
    flags += Shell32.SHGFI_LARGEICON ;
    } Shell32.SHGetFileInfo( name, 
    Shell32.FILE_ATTRIBUTE_NORMAL, 
    ref shfi, 
    (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi), 
    flags ); // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
    System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
    User32.DestroyIcon( shfi.hIcon ); // Cleanup
    return icon;
    } /// <summary>
    /// Used to access system folder icons.
    /// </summary>
    /// <param name="size">Specify large or small icons.</param>
    /// <param name="folderType">Specify open or closed FolderType.</param>
    /// <returns>System.Drawing.Icon</returns>
    public static System.Drawing.Icon GetFolderIcon( IconSize size, FolderType folderType )
    {
    // Need to add size check, although errors generated at present!
    uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES; if (FolderType.Open == folderType)
    {
    flags += Shell32.SHGFI_OPENICON;
    }

    if (IconSize.Small == size)
    {
    flags += Shell32.SHGFI_SMALLICON;

    else 
    {
    flags += Shell32.SHGFI_LARGEICON;
    } // Get the folder icon
    Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
    Shell32.SHGetFileInfo( null, 
    Shell32.FILE_ATTRIBUTE_DIRECTORY, 
    ref shfi, 
    (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi), 
    flags ); System.Drawing.Icon.FromHandle(shfi.hIcon); // Load the icon from an HICON handle // Now clone the icon, so that it can be successfully stored in an ImageList
    System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon( shfi.hIcon ); // Cleanup
    return icon;
    }
    }

    /// <summary>
    /// Wraps necessary Shell32.dll structures and functions required to retrieve Icon Handles using SHGetFileInfo. Code
    /// courtesy of MSDN Cold Rooster Consulting case study.
    /// </summary>
    /// 
      

  4.   

    // This code has been left largely untouched from that in the CRC example. The main changes have been moving
    // the icon reading code over to the IconReader type.
    public class Shell32  
    {

    public const int  MAX_PATH = 256;
    [StructLayout(LayoutKind.Sequential)]
    public struct SHITEMID
    {
    public ushort cb;
    [MarshalAs(UnmanagedType.LPArray)]
    public byte[] abID;
    } [StructLayout(LayoutKind.Sequential)]
    public struct ITEMIDLIST
    {
    public SHITEMID mkid;
    } [StructLayout(LayoutKind.Sequential)]
    public struct BROWSEINFO 

    public IntPtr hwndOwner; 
    public IntPtr pidlRoot; 
    public IntPtr  pszDisplayName;
    [MarshalAs(UnmanagedType.LPTStr)] 
    public string  lpszTitle; 
    public uint  ulFlags; 
    public IntPtr lpfn; 
    public int lParam; 
    public IntPtr  iImage; 
    }  // Browsing for directory.
    public const uint BIF_RETURNONLYFSDIRS   = 0x0001;
    public const uint BIF_DONTGOBELOWDOMAIN  = 0x0002;
    public const uint BIF_STATUSTEXT         = 0x0004;
    public const uint BIF_RETURNFSANCESTORS  = 0x0008;
    public const uint BIF_EDITBOX            = 0x0010;
    public const uint BIF_VALIDATE           = 0x0020;
    public const uint BIF_NEWDIALOGSTYLE     = 0x0040;
    public const uint BIF_USENEWUI           = (BIF_NEWDIALOGSTYLE | BIF_EDITBOX);
    public const uint BIF_BROWSEINCLUDEURLS  = 0x0080;
    public const uint BIF_BROWSEFORCOMPUTER  = 0x1000;
    public const uint BIF_BROWSEFORPRINTER   = 0x2000;
    public const uint BIF_BROWSEINCLUDEFILES = 0x4000;
    public const uint BIF_SHAREABLE          = 0x8000; [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO

    public const int NAMESIZE = 80;
    public IntPtr hIcon; 
    public int iIcon; 
    public uint dwAttributes; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
    public string szDisplayName; 
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=NAMESIZE)]
    public string szTypeName; 
    }; public const uint SHGFI_ICON = 0x000000100;     // get icon
    public const uint SHGFI_DISPLAYNAME = 0x000000200;     // get display name
    public const uint SHGFI_TYPENAME           = 0x000000400;     // get type name
    public const uint SHGFI_ATTRIBUTES         = 0x000000800;     // get attributes
    public const uint SHGFI_ICONLOCATION       = 0x000001000;     // get icon location
    public const uint SHGFI_EXETYPE            = 0x000002000;     // return exe type
    public const uint SHGFI_SYSICONINDEX       = 0x000004000;     // get system icon index
    public const uint SHGFI_LINKOVERLAY        = 0x000008000;     // put a link overlay on icon
    public const uint SHGFI_SELECTED           = 0x000010000;     // show icon in selected state
    public const uint SHGFI_ATTR_SPECIFIED     = 0x000020000;     // get only specified attributes
    public const uint SHGFI_LARGEICON          = 0x000000000;     // get large icon
    public const uint SHGFI_SMALLICON          = 0x000000001;     // get small icon
    public const uint SHGFI_OPENICON           = 0x000000002;     // get open icon
    public const uint SHGFI_SHELLICONSIZE      = 0x000000004;     // get shell size icon
    public const uint SHGFI_PIDL               = 0x000000008;     // pszPath is a pidl
    public const uint SHGFI_USEFILEATTRIBUTES  = 0x000000010;     // use passed dwFileAttribute
    public const uint SHGFI_ADDOVERLAYS        = 0x000000020;     // apply the appropriate overlays
    public const uint SHGFI_OVERLAYINDEX       = 0x000000040;     // Get the index of the overlay public const uint FILE_ATTRIBUTE_DIRECTORY  = 0x00000010;  
    public const uint FILE_ATTRIBUTE_NORMAL     = 0x00000080;   [DllImport("Shell32.dll")]
    public static extern IntPtr SHGetFileInfo(
    string pszPath,
    uint dwFileAttributes,
    ref SHFILEINFO psfi,
    uint cbFileInfo,
    uint uFlags
    );
    } /// <summary>
    /// Wraps necessary functions imported from User32.dll. Code courtesy of MSDN Cold Rooster Consulting example.
    /// </summary>
    public class User32
    {
    /// <summary>
    /// Provides access to function required to delete handle. This method is used internally
    /// and is not required to be called separately.
    /// </summary>
    /// <param name="hIcon">Pointer to icon handle.</param>
    /// <returns>N/A</returns>
    [DllImport("User32.dll")]
    public static extern int DestroyIcon( IntPtr hIcon );
    }
    /// <summary>
    /// IconListManager 的摘要说明。
    /// </summary>
    public class IconListManager
    {
    private Hashtable _extensionList = new Hashtable();
    private System.Collections.ArrayList _imageLists = new ArrayList(); //will hold ImageList objects
    private IconReader.IconSize _iconSize;
    bool ManageBothSizes = false; //flag, used to determine whether to create two ImageLists.
      

  5.   

    /// <summary>
    /// Creates an instance of <c>IconListManager</c> that will add icons to a single <c>ImageList</c> using the
    /// specified <c>IconSize</c>.
    /// </summary>
    /// <param name="imageList"><c>ImageList</c> to add icons to.</param>
    /// <param name="iconSize">Size to use (either 32 or 16 pixels).</param>
    public IconListManager(System.Windows.Forms.ImageList imageList, IconReader.IconSize iconSize )
    {
    // Initialise the members of the class that will hold the image list we're
    // targeting, as well as the icon size (32 or 16)
    _imageLists.Add( imageList );
    _iconSize = iconSize;
    }

    /// <summary>
    /// Creates an instance of IconListManager that will add icons to two <c>ImageList</c> types. The two
    /// image lists are intended to be one for large icons, and the other for small icons.
    /// </summary>
    /// <param name="smallImageList">The <c>ImageList</c> that will hold small icons.</param>
    /// <param name="largeImageList">The <c>ImageList</c> that will hold large icons.</param>
    public IconListManager(System.Windows.Forms.ImageList smallImageList, System.Windows.Forms.ImageList largeImageList )
    {
    //add both our image lists
    _imageLists.Add( smallImageList );
    _imageLists.Add( largeImageList ); //set flag
    ManageBothSizes = true;
    } /// <summary>
    /// Used internally, adds the extension to the hashtable, so that its value can then be returned.
    /// </summary>
    /// <param name="Extension"><c>String</c> of the file's extension.</param>
    /// <param name="ImageListPosition">Position of the extension in the <c>ImageList</c>.</param>
    private void AddExtension( string Extension, int ImageListPosition )
    {
    _extensionList.Add( Extension, ImageListPosition );
    } /// <summary>
    /// Called publicly to add a file's icon to the ImageList.
    /// </summary>
    /// <param name="filePath">Full path to the file.</param>
    /// <returns>Integer of the icon's position in the ImageList</returns>
    public int AddFileIcon( string filePath )
    {
    // 检查文件是否存在,如果不存在,抛出异常,本软件只是取图标,故忽略原程序中的本行
    //if (!System.IO.File.Exists( filePath )) throw new System.IO.FileNotFoundException("File does not exist");

    // 拆分文件名,以得到扩展名
    string[] splitPath = filePath.Split(new Char[] {'.'});
    string extension = (string)splitPath.GetValue( splitPath.GetUpperBound(0) );

    //是否已经存在该扩展名对应的图标,如果已经存在,返回索引
    if (_extensionList.ContainsKey( extension.ToUpper() ))
    {
    return (int)_extensionList[extension.ToUpper()]; //return existing index

    else 
    {
    // 如果不存在则添加,并返回索引 int pos = ((ImageList)_imageLists[0]).Images.Count; //store current count -- new item's index if (ManageBothSizes == true)
    {
    //managing two lists, so add it to small first, then large
    ((ImageList)_imageLists[0]).Images.Add( IconReader.GetFileIcon( filePath, IconReader.IconSize.Small, false ) );
    ((ImageList)_imageLists[1]).Images.Add( IconReader.GetFileIcon( filePath, IconReader.IconSize.Large, false ) );

    else
    {
    //only doing one size, so use IconSize as specified in _iconSize.
    ((ImageList)_imageLists[0]).Images.Add( IconReader.GetFileIcon( filePath, _iconSize, false ) ); //add to image list
    } AddExtension( extension.ToUpper(), pos ); // add to hash table
    return pos;
    }
    } /// <summary>
    /// Clears any <c>ImageLists</c> that <c>IconListManager</c> is managing.
    /// </summary>
    public void ClearLists()
    {
    foreach( ImageList imageList in _imageLists )
    {
    imageList.Images.Clear(); //clear current imagelist.
    }

    _extensionList.Clear(); //empty hashtable of entries too.
    }
    }
    }
    }