程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> C#中調用API獲得指定文件的圖標

C#中調用API獲得指定文件的圖標

編輯:.NET實例教程
最近在寫一個媒體管理的服務器程序。裡面要用到了一個獲得指定文件的集成圖標的。所以就寫了一個用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;
        };
    }
}

 

調用方法:

 



 /**//// <summary>
        /// 獲得指定路徑下面的所有文件及文件夾
        /// </summary>
        /// <param name="path">路徑</param>
        /// <returns></returns>
        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();
                 &nbsitem = 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved