ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C#中调用API获得指定文件的图标

C#中调用API获得指定文件的图标

原创 Linux操作系统 作者:freshairpeng 时间:2009-03-05 22:54:10 0 删除 编辑

最近在写一个媒体管理的服务器程序。里面要用到了一个获得指定文件的集成图标的。所以就写了一个用C#中调用API来获得指定文件的图标的代码,代码如下:

导入API的类:

 

using System;
using System.Runtime.InteropServices;

namespace MediaClient
...{
    public class Win32
    ...{
        [DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
        public static extern int ExtractIcon(IntPtr hInst,string lpFileName,int nIndex);

        [DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
        public static extern IntPtr SHGetFileInfo(string pszPath,uint dwFileAttribute,ref SHFILEINFO psfi,uint cbSizeFileInfo,uint Flags);

        [DllImport("User32.dll", EntryPoint = "DestroyIcon")]
        public static extern int DestroyIcon(IntPtr hIcon);

        [StructLayout(LayoutKind.Sequential)]
        public struct SHFILEINFO
        ...{
            public IntPtr hIcon;
            public IntPtr iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        };
    }
}

 

调用方法:

 

 

 /**////


        /// 获得指定路径下面的所有文件及文件夹
        ///

        /// 路径
        ///
        public ArrayList GetListViewItem(string path,ImageList imglist)
        ...{
            Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();
            try
            ...{
                string[] dirs = Directory.GetDirectories(path);
                string[] files = Directory.GetFiles(path);
                ArrayList itemarr = new ArrayList();

                for (int i = 0; i < dirs.Length; i++)
                ...{
                    string[] info = new string[4];
                    DirectoryInfo dir = new DirectoryInfo(dirs[i]);
                    //获得图标
                    Win32.SHGetFileInfo(dirs[i],
                                        (uint)0x80,
                                        ref shfi,
                                        (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                        (uint)(0x100 | 0x400)); //取得Icon和TypeName
                    //添加图标
                    imglist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
                    info[0] = dir.Name;
                    info[1] = "";
                    info[2] = "文件夹";
                    info[3] = dir.LastWriteTime.ToString();
                    ListViewItem item = new ListViewItem(info,dir.Name);
                    itemarr.Add(item);
                    //销毁图标
                    Win32.DestroyIcon(shfi.hIcon);
                }
                for (int i = 0; i < files.Length; i++)
                ...{
                    string[] info = new string[4];
                    FileInfo fi = new FileInfo(files[i]);
                    //获得图标
                    Win32.SHGetFileInfo(files[i],
                                        (uint)0x80,
                                        ref shfi,
                                        (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                        (uint)(0x100 | 0x400)); //取得Icon和TypeName
                    //添加图标
                    imglist.Images.Add(fi.Name,(Icon)Icon.FromHandle(shfi.hIcon).Clone());
                    info[0] = fi.Name;
                    info[1] = fi.Length.ToString();
                    info[2] = fi.Extension.ToString();
                    info[3] = fi.LastWriteTime.ToString();
                    ListViewItem item = new ListViewItem(info,fi.Name);
                    itemarr.Add(item);
                    //销毁图标
                    Win32.DestroyIcon(shfi.hIcon);
                }
                return itemarr;
               
            }
            catch
            ...{
                return null;
            }
        }
 

uFlags常数:
  SHGFI_ICON                                   =   0x100  
  SHGFI_DISPLAYNAME                   =   0x200,            
  SHGFI_TYPENAME                        =   0x400,          
  SHGFI_ATTRIBUTES                     =   0x800,        
  SHGFI_ICONLOCATION                 =   0x1000,            
  SHGFI_EXETYPE                           =   0x2000,            
  SHGFI_SYSICONINDEX                 =   0x4000,          
  SHGFI_LINKOVERLAY                  =   0x8000,            
  SHGFI_SELECTED                        =   0x10000,            
  SHGFI_ATTR_SPECIFIED             =   0x20000,            
  SHGFI_LARGEICON                      =   0x0,            
  SHGFI_SMALLICON                     =   0x1,            
  SHGFI_OPENICON                        =   0x2,            
  SHGFI_SHELLICONSIZE              =   0x4,            
  SHGFI_PIDL                                 =   0x8,            
  SHGFI_USEFILEATTRIBUTES      =   0x10

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14766526/viewspace-562964/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-02-25

  • 博文量
    69
  • 访问量
    246282